更新可以通过x ++中的多个联接检索的记录 [英] Update records that can be retrieved by multiple joins in x++

查看:118
本文介绍了更新可以通过x ++中的多个联接检索的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个完整的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

正如我所知,在有跨公司的情况下更新记录不是一个好主意.考虑到我当前的脚本,您能否就最佳实践方式提供专家建议.

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.

这是脚本

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

"无法编辑库存模块参数(InventTableModule)中的记录. 从未选择该记录."

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

作为解决方案,基于此链接如何更新/插入/删除CrossCompany a>,我尝试遵循相同的方法,这是修改后的脚本

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

我是x ++的新手,希望我能获得有关最佳实践的专家意见.

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

谢谢.

推荐答案

首先,不要尝试进行跨公司更新,否则必然会失败. 在当前公司中进行更新,然后将该脚本应用于其他相关公司.

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.

修复了一些问题:

  • 尝试更新通过存在连接找到的记录将不起作用,因此会出现错误.
  • 根据记录进行测试是多余的,如果没有找到,则不会进入循环
  • 使用大笔交易

还将更新放在一个内部函数中,这将使在多个公司中进行更新变得容易.有关在所有公司中的操作方式,请参见此答案.

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

这篇关于更新可以通过x ++中的多个联接检索的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆