简体   繁体   English

更新可以通过x ++中的多个联接检索的记录

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

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 因此,我有一个完整的x ++脚本,该脚本旨在根据具有多个联接的select查询和使用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." “无法在库存模块参数(InventTableModule)中编辑记录。从未选择该记录。”

As solution, based on this link How to Update/Insert/Delete CrossCompany , i tried following the same, this is the modified script 作为解决方案,基于此链接How to Update / Insert / Delete CrossCompany ,我尝试遵循同样的方法,这是修改后的脚本

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. 我是x ++的新手,希望我能获得有关最佳实践的专家意见。

Thanks in advance. 提前致谢。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM