Monday, February 17, 2020

Using CrossCompany and ChangeCompany keywords in D365 Finance and Operations

Hello People,

Hope you are doing good. In today's post, will discuss about the requirement(a Script) which will update a record (if available ) in every legal entity by looping through them and increment the LineNum value with ONE.

This Script can also be used, when a data entity doesn't have Unique index/ Primary index on TestTable(standard table) and you want to customize it  by adding new LineNum field to make it as a Unique index.

Follow below sample code snippet on same.


class updateLineNumInTestTable
{       
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {   
        TestTable        testTable;// Note: TestTable -Save Data Per Company property is set to  YES
        DataArea        dataArea; // Standard table which will store all legal entities

        while select id from dataArea
        {
            container        conCompanies    = str2con(dataArea.id);
            LineNumber   lineCount            = 1;
           
            ttsbegin;
            while select forupdate crosscompany : conCompanies
                LineNum, DataAreaId, RecId from testTable
                    where !testTable.LineNum
            {
                changecompany(con2Str(conCompanies))
                {
                    testTable.LineNum = lineCount;
                    testTable.doUpdate();
                    lineCount++;
                }
            }
            ttscommit;

        }
        info(strFmt('Process Completed'));
    }
}

Hope this post helps you to use the keywords CrossCompany and changeCompany. Happy Daxing.

No comments:

Post a Comment