Axapta 更新可通过x++;

Axapta 更新可通过x++;,axapta,x++,dynamics-ax-2012,dynamics-ax-2012-r2,Axapta,X++,Dynamics Ax 2012,Dynamics Ax 2012 R2,因此,我有一个完整的x++脚本,该脚本旨在根据检索到的结果集来更新记录,该结果集是使用带有多个联接的select查询并使用crosscompany创建的 正如我被告知的那样,当公司发生冲突时,更新记录不是一个好主意。考虑到我目前的剧本,你能就如何以最佳实践方式完成这项工作给出专家建议吗 这是剧本 static void UpdateSample(Args _args) { InventTable a; InventTableModule b; EcoResProduct c

因此,我有一个完整的x++脚本,该脚本旨在根据检索到的结果集来更新记录,该结果集是使用带有多个联接的select查询并使用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;
              ttsBegin;
              b.LineDisc= 'something';
              b.update();
              ttscommit;
             }
        }
     info(strfmt("total record/s updated : %1",i));
}
当我在上面运行时,我遇到了这个错误

“无法编辑库存模块参数(InventTableModule)中的记录。”。 从未选择过该记录。“

作为解决方案,基于此链接,我尝试遵循相同的方法,这是修改后的脚本

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));
}
但我在这行有语法错误

 b.selectForUpdate(true);
我是新来的x++,希望我能得到专家的建议,在这样做的最佳实践


提前感谢。

首先,不要尝试跨公司进行更新,它注定会失败。 在当前公司中执行更新,然后将脚本应用于其他相关公司

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();
}
修正了一些问题:

  • 尝试更新使用exists join找到的记录将不起作用,因此出现错误
  • 对发现的记录进行测试是多余的,如果没有发现,则不会进入循环
  • 使用大型事务
另外,将更新放在一个内部函数中,这将使在多个公司进行更新变得容易。请参阅所有公司的“如何做”

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

首先,不要试图做跨公司更新,这是注定要失败的。 在当前公司中执行更新,然后将脚本应用于其他相关公司

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();
}
修正了一些问题:

  • 尝试更新使用exists join找到的记录将不起作用,因此出现错误
  • 对发现的记录进行测试是多余的,如果没有发现,则不会进入循环
  • 使用大型事务
另外,将更新放在一个内部函数中,这将使在多个公司进行更新变得容易。请参阅所有公司的“如何做”

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


您的try/catch没有任何用处,请将其删除。您的代码未编译,inv未定义,也未在循环中找到。请使用根据标准应用程序编译的源代码。可能重复的代码确实需要使用tab键!您的try/catch没有任何用处,请将其删除。您的代码未编译,inv未定义,也未在循环中找到。请使用根据标准应用程序编译的源代码。可能重复的代码确实需要使用tab键!因此,指定其所在dataareaid/位置的字段将被删除。永远不要这样做,请使用changecompany(或在当前公司中应用)。因此,行changecompany、dataareaid/位置规范应该在('xxx')中,对吗?而不是在内部查询中指定好的猜测。。。。我猜你真的只想在一家公司更新。您必须了解如何使用用户界面更改公司。是的,只是为了提供背景信息,只有在发布项目之前,由于设置不正确,这些项目主控形状在特定位置的linedisc中的值才会出现预期错误。因此,需要首先查找这些项,并且只能通过使用select查询中的联接来查找。因此,用于指定要删除的dataareaid/位置的函数将被删除。永远不要这样做,请使用changecompany(或在当前公司中应用)。所以行changecompany、dataareaid/位置规范应该在('xxx')中,对吗?而不是在内部查询中指定好的猜测。。。。我猜你真的只想在一家公司更新。您必须了解如何使用用户界面更改公司。是的,只是为了提供背景信息,只有在发布项目之前,由于设置不正确,这些项目主控形状在特定位置的linedisc中的值才会出现预期错误。因此需要首先查找这些项,并且只能通过使用select查询中的联接来查找。