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.