crossCompany and changeCompany in AX 2012

Hi Folks,

Today, in this article we will understand the use of crossCompany and changeCompany in AX 2012.

Let's start...

crossCompany:

You can fetch the data from tables across companies using crossCompany keyword in X++ select queries. Suppose you have a requirement to get the data from multiple companies in AX 2012 using X++ then in this scenario we can use crossCompany.

You can also use containers to fetch data from subset of companies.

For example:


public void TestCrossCompanyUse()
{
    CustTable       custTable;
    container       desiredCompanies = ['A', 'B']; // A and B are companies name
    int             iCountCompanies  = 0;
 
    while select crossCompany : desiredCompanies * from custTable
    {
        info(strFmt("%1, %2", custTable.AccountNum, custTable.dataAreaId));
     
        iCountCompanies++;
        if (iCountCompanies >= 1000 )
        {
            break;
        }
    }
}

Note: In X++ code you can use the Query .allowCrossCompany property method to achieve the same result as you can with the crossCompany keyword on a select statement. The calls to the Query .addCompanyRange method are the same as appending a container of companies to the crossCompany keyword.

changeCompany:

You can use changeCompany function in X++ to change companies. Suppose you are login with Company 'A' and you want to fetch the data for company 'B'. In this scenario we can use changeCompany.

For example:


static void TestChangeCompanyUse(Args _args)
{
    CustTable       custTable;
 
    // We are login with company 'A' and want to fetch the data for company 'B'
    changeCompany('B')
    {
        custTable = null; // To clear the old memory
     
        while select * from custTable
        {
            info(strFmt("%1, %2", custTable.AccountNum, custTable.dataAreaId));
        }
    }
}

We hope this article helped you to understand the use of crossCompany and changeCompany using X++ in AX 2012. Please comment your valuable feedback for this article...

Author:
Chirag Gupta
Microsoft Dynamics D365/AX 2012
Senior Technical Consultant at IBM Bangalore

Date:
4-Apr-2020

Happy Learning !!

Comments

Popular posts from this blog

[Solved] : Error: String or binary data would be truncated. Cannot execute a data definition language command on Vendor (VendLedgerReconciliationTmp)

AX 2012 - Command prompt compilation / AxBuild.exe for Parallel Compile on AOS of X++ to P-code / Parallel Compilation

AX 2012 - How to send SSRS report in email as an attachment using X++

AX 2012 - How to stop AOS when services got stuck at stopping status

AX 2012 - How to make a form field mandatory

Error: There was no endpoint listening at net.tcp://your_server_name:8201/Dynamics AX/Services…

Fetch vendor address using X++ in AX 2012

AX 2012 - How to print current date and time on SSRS Report

[Error] - Error in unit of amount in currency KES. Must be rounded to 0.01 | The unit of 37.563.5343100000000000 KES being posted to account 32710001-- is outside the current penny rounding threshold | Posting has been canceled

[Solved] : AX 2012 - No connection could be made because the target machine actively refused it 192.168.10.249:8201