27 March 2017

What is wrong: crossCompany update job in AX 2012

I want to challenge you today to find an error in the following update job. Do not pay attention to the functional part. I agree, the job can be rewritten, but let's assume it must be done this way.
static void crossCompanyUpdate(Args _args)
{
    CustTable   custTable;
    CustGroup   custGroup;
    PaymTermId  oldPaymTermId = 'Net10', newPaymTermId = 'Net11';
    
    ttsBegin;

    // find all companies with oldPaymTermId
    while select crossCompany custGroup
        group by custGroup.dataAreaId
        where custGroup.PaymTermId == oldPaymTermId
    {
        changeCompany(custGroup.dataAreaId)
        {
            // disable update method
            custTable.skipDataMethods(true);

            // set newPaymTermId
            update_recordSet crossCompany custTable
                setting PaymTermId = newPaymTermId
                where custTable.PaymTermId == oldPaymTermId;
        }
    }

    ttsCommit;
}
Post your ideas into comments. I will open comments and post my solution in one week. Have fun :-)

Solution
There is no compile or runtime error, but records are updated only in one company. Table variable must be set to null within changeCompany statement, marked in orange below:
static void crossCompanyUpdate(Args _args)
{
    CustTable   custTable;
    CustGroup   custGroup;
    PaymTermId  oldPaymTermId = 'Net10', newPaymTermId = 'Net11';
    
    ttsBegin;

    // find all companies with oldPaymTermId
    while select crossCompany custGroup
        group by custGroup.dataAreaId
        where custGroup.PaymTermId == oldPaymTermId
    {
        changeCompany(custGroup.dataAreaId)
        {
            custTable = null;

            // disable update method
            custTable.skipDataMethods(true);

            // set newPaymTermId
            update_recordSet crossCompany custTable
                setting PaymTermId = newPaymTermId
                where custTable.PaymTermId == oldPaymTermId;
        }
    }

    ttsCommit;
}

4 comments:

  1. Anonymous27/3/17 12:29

    custTable = null ;)

    ReplyDelete
    Replies
    1. It is correct, thank you for the comment! :-)

      Delete
  2. My ideas is static void crossCompanyUpdate(Args _args)
    {
    CustTable custTable;
    CustGroup custGroup;
    PaymTermId oldPaymTermId = 'Net10', newPaymTermId = 'Net11';
    ttsBegin;
    // find all companies with oldPaymTermId
    while select crossCompany custGroup
    group by custGroup.dataAreaId
    where custGroup.PaymTermId == oldPaymTermId
    {
    changeCompany(custGroup.dataAreaId)
    {
    custTable = null;
    // disable update method
    custTable.skipDataMethods(true);
    // set newPaymTermId
    update_recordSet custTable
    setting PaymTermId = newPaymTermId
    where custTable.PaymTermId == oldPaymTermId;
    }
    }
    ttsCommit;
    } Please check that code. I can't check because I have an AX 2009 version system.

    ReplyDelete
    Replies
    1. You are absolutely right, thank you for the comment! :-)

      Delete