Update records that can be retrieved by multiple joins in x++

后端 未结 1 1123
梦如初夏
梦如初夏 2021-01-29 00:55

So I have a complete x++ script that aims to update records based on the retrieved result set made with select query with multiple joins and using crosscompany<

1条回答
  •  -上瘾入骨i
    2021-01-29 01:22

    First off, do not try to do update cross company, it is bound to fail. Make the update work in current company, then apply the script to other relevant companies.

    Fixed a few things:

    • Trying to update a record found with exists join will not work, hence your error.
    • Testing on record found is redundant, the loop will not be entered if none is found
    • Use a large transaction

    Also put the update in an inner function, this will make it easy to update in more than one company. See this answer on how to do in all companies.

    static void UpdateSample(Args _args)
    {
        void doIt()
        {
            InventTable  a;
            InventTableModule b;
            EcoResProduct c;
            EcoResProductCategory d;
            EcoResCategory e;
            EcoResCategoryHierarchy f;
            int i;
            ttsBegin;
            while select a
                join forUpdate b where a.ItemId  == b.ItemId  
                exists join c where a.Product  == c.RecId
                exists join d where c.RecId  == d.Product
                exists join e where d.Category  == e.RecId
                exists join f where d.CategoryHierarchy  == f.RecId
                && b.ModuleType  == 2
                && b.LineDisc  == ''
                && f.name == 'EXAMPLE'
                &&(e.name == 'sample1' || e.name == 'sample2' || e.name == 'sample3')
            {
                ++i;
                b.LineDisc= 'something'; 
                b.update();
            }
            ttsCommit;
            info(strfmt("total record/s updated : %1", i));
        }
        changecompany ('XXX')
            doIt();
    }
    

    0 讨论(0)
提交回复
热议问题