1

I added a checkbox in PurchParameters table which name is setExchRateVal and I want to mark true this field in my all companies without sql operations.

How can i do this in AX with job?

I tried this but it's not done,

PurchParameters purchParameters ;

while select forUpdate crossCompany purchParameters
{
    purchParameters.setExchRateVal = NoYes::Yes;
    purchParameters.update();
    //info(strFmt("%1 - %2", purchParameters.SetExchRateVal, purchParameters.dataAreaId));
}

AX ERROR : Update operations are not allowed across companies.

Mumble
  • 141
  • 1
  • 8
  • 1
    What code have you tried? Any sample? – Alex Kwitny May 01 '20 at 15:31
  • 1
    Check out [crossCompany/changeCompany](https://learn.microsoft.com/en-us/dynamicsax-2012/developer/cross-company-data-modification-using-x) and the [DataArea](https://learn.microsoft.com/en-us/previous-versions/dynamics/ax-2012/reference/gg887932(v=ax.60)?redirectedfrom=MSDN) table. – FH-Inway May 01 '20 at 15:42
  • Thank you @FH-Inway, i used changeCompany in while statement and it's done – Mumble May 01 '20 at 15:50

1 Answers1

3

The error is clear. You can't do crossCompany and updates in the same select query. Method 2 below is closer to what you're doing. When updating parameter tables, it can be done a few ways because of the Key on the table.

See below:

PurchParameters purchParametersUpdate;
PurchParameters purchParametersSeek;
DataArea        dataArea;

// Method 1
ttsBegin;
while select dataArea
{
    changeCompany(dataArea.id)
    {
        purchParametersUpdate = PurchParameters::find(true);
        purchParametersUpdate.setExchRateVal = NoYes::Yes;
        purchParametersUpdate.update();
    }
}
ttsCommit;


// Method 2
ttsBegin;
while select crossCompany purchParametersSeek
{
    purchParametersUpdate = null;

    select firstOnly forUpdate purchParametersUpdate
        where purchParametersUpdate.RecId == purchParametersSeek.RecId;

    if (purchParametersUpdate)
    {
        //purchParametersUpdate.setExchRateVal = NoYes::Yes;
        purchParametersUpdate.update();
    }
}
ttsCommit;

info("Done");
Alex Kwitny
  • 11,211
  • 2
  • 49
  • 71