December 07, 2012

Change company from the current company in X++ code

Change company from the current company in X++ code

If in any point you need to change change company during the code, This example will helps you to change the company from current company in x++ code.

static void main()
{
  CustTable custTable;
  ;
  //Assume that you are running in company 'DAT'.
  changeCompany('DAT1') //Default company is now 'DAT1'.
  {
    custTable = null;
    while select custTable
    {
       //custTable is now selected in company 'DAT1'.
    }
  }
 //Default company is again set back to 'DAT'.
 changeCompany('DAT2') //Default company is now 'DAT2'.
 {
   //Clear custTable to let the select work
   //on the new default company.
   custTable = null;
   while select custTable
    {
      //custTable is now selected in company 'DAT2'.
    }
 }
//Default company is again 'DAT'.

}

-Harry.