Skip to main content

CrossCompany keyword in X++



Friends,

Going to discuss a simple topic which sometimes play vital role in MSD AX.
CrossCompany is the Keyword to get the data from another company.

In X++, there is a new keyword crossCompany you can use in a select statement. Additionally, you can add a container with all the company ID’s you wants to include in your select statement.
So, your select statement could look like that:
While select CustTable crossCompany : [Test, DAT]
{
... Your Code...
}
(Or)

static void CrossCompany_testing(Args _args)
{
CustTable custTable;
;

while select crossCompany * from custTable
{
print custTable.AccountNum,' : ',custTable.Name,' : ',custTable.dataAreaId;
}
pause;

}
And let me also show you simple example wherein you can copy your data from one company to another company just in a simple click. So here is the code which you can implement wherever necessary.
This code will generate the dialog which asks for user input for ‘fromCompany’ to ‘Tocompany’.
static void CompanyCopyTable(Args _args)
{

    Dialog          dialog;
    DialogField     FromCompany, ToCompany;
    str             FromComp,ToComp;
    CustTable       custTable1, custTable2;
    Args            args = new Args();
    ;

    dialog = new Dialog("CrossCompanyExample");

    dialog.addText("Select the source company:");

    FromCompany = dialog.addField(typeid(dataAreaId));

    dialog.addText("Select the destination company:");

    ToCompany = dialog.addField(typeid(dataAreaId));

    dialog.run();

    if(dialog.closedOk() == true)
    {

        FromComp = FromCompany.value();
        info(FromComp);
        ToComp = ToCompany.value();
        info(ToComp);

        changecompany(FromComp)
        {
            while select CustTable1
            {
                changecompany(ToComp)
                {
                    custTable2 = null;
                    buf2buf(CustTable1, custTable2);
                    if (!custTable2.validateWrite())
                    {
                        throw Exception::Error;
                    }
                    custTable2.insert();

                }
            }
        }
    }

Comments