x++ script that do not return correct count

霸气de小男生 提交于 2020-01-06 06:36:52

问题


I have a x++ script which aims to count records from a select query and later on be updated.

This is the original question for reference: convert SQL Query with Join to X++ Dynamics AX scripting

Initially, I have a SQL Query counterpart of it and it is resulting to 50 rows / records, when I convert it to X++ , it is not counting or extracting the same number of records,

Here is is the x++ script

static void Job(Args _args)
{

    Table1 table1;
    Table2 table2;
    Table3 table3;
    Table4 table4;
    Table5 table5;
    int i = 0;
    while select forUpdate table1  
       join table2 where table1.field1 == table2.field1 
       join table3 where table1.field2 == table3.field2
       join table4 where table3.field3 == table4.field3
       join table5 where table3.category == table5.recid
       && table1.location  == 'asia' && table2.modtye == 2
       && table3.discount == 'sample' 
       && table4.name ==  'hello' 
       &&(table5.name == 'one' || table5.name == 'two' || table5.name == 'three')                
    {    
            if (table1)    
            {
                 i = i + 1;    
            }    
    }    
    info(strfmt("Total : %1",i));    
}

Pls help, where did i go wrong it think it's with this part

if (table1)

I also tried trimming down the codes to know where the problem arise,

 while select forUpdate table1  
           join table2 where table1.field1 == table2.field1 
           && table1.location  == 'asia' && table2.modtye == 2

This part dont return result already... when I include the

 && table1.location  == 'asia' && table2.modtye == 2

So i think, the problem is there, but what is wrong with the code?


I based my codes actually from this tutorial link

https://community.dynamics.com/ax/b/dynamicsaxtipoftheday/archive/2014/09/05/quickly-update-data-through-x-scripts


回答1:


I suggest a simple explanation, maybe the SQL returns rows from a several companies or partitions?
AX by default returns row for the current partition and company curext() only.

If you use the crosscompany option to the select it will scan cross companies:

while select crosscompany table1 ...

You do not need to test whether table1 is found, if not found it will not enter the loop.

Also, if your sole purpose is to count the number of records it is wasteful to count manually, a single select will do:

select firstOnly /*crosscompany*/ count(RecId) from table1  
   exists join table2 where table1.field1 == table2.field1 
   exists join table3 where table1.field2 == table3.field2
   exists join table4 where table3.field3 == table4.field3
   exists join table5 where table3.category == table5.recid
     && table1.location  == 'asia' && table2.modtye == 2
     && table3.discount == 'sample' 
     && table4.name ==  'hello' 
     &&(table5.name == 'one' || table5.name == 'two' || table5.name == 'three');
info(strfmt("Total : %1", table1.RecId));


来源:https://stackoverflow.com/questions/56319538/x-script-that-do-not-return-correct-count

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