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

时间秒杀一切 提交于 2020-01-11 14:28:29

问题


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

As I have been told, it is not a good idea to update records when there is crosscompany. Can you give expert advice on how to do it the best practice way considering my current script.

here is the script

static void UpdateSample(Args _args)
{

   InventTable  a;
   InventTableModule b;
   EcoResProduct c;
   EcoResProductCategory d;
   EcoResCategory e;
   EcoResCategoryHierarchy f;
   int i = 0;

    while select crossCompany  a
    exists join 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
    && a.dataAreaId == 'DAT' && b.ModuleType  == 2
    && b.LineDisc  == ''
    && f.name == 'EXAMPLE'
    &&(e.name == 'sample1' || e.name == 'sample2' || e.name == 'sample3')

       {
        if (a)
             {
              i = i + 1;
              ttsBegin;
              b.LineDisc= 'something';
              b.update();
              ttscommit;
             }
        }
     info(strfmt("total record/s updated : %1",i));
}

When I run above, am having this error

"Cannot edit a record in Inventory module parameters (InventTableModule). The record has never been selected."

As solution, based on this link How to Update/Insert/Delete CrossCompany, i tried following the same, this is the modified script

static void UpdateSample(Args _args)
{
   InventTable  a;
   InventTableModule b;
   EcoResProduct c;
   EcoResProductCategory d;
   EcoResCategory e;
   EcoResCategoryHierarchy f;
   int i = 0;

    while select crossCompany  a
    exists join 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
    && a.dataAreaId == 'DAT' && b.ModuleType  == 2
    && b.LineDisc  == ''
    && f.name == 'EXAMPLE'
    &&(e.name == 'sample1' || e.name == 'sample2' || e.name == 'sample3')

       {
       if (a)
             {
              i = i + 1;
              b.LineDisc= 'something'; 
              b.selectForUpdate(true);
              ttsBegin;
              b.update();
              ttsCommit;
             }
        }
     info(strfmt("total record/s updated : %1",i));
}

But I am having SYNTAX ERROR on this line

 b.selectForUpdate(true);

I am new to x++, hope i can get expert advice about the best practice in doing this.

Thanks in advance.


回答1:


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();
}


来源:https://stackoverflow.com/questions/56323820/update-records-that-can-be-retrieved-by-multiple-joins-in-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!