How to change company by the X++ code in AX 2009 ??

sometimes you need to change company by code to get some information from company to put it in another one like update the price for company X by the price you get from company Y and this example will explain :
void clicked()
{
inventtable _inventtable;
InventTableModule invtm;
InventTableModule invtmhaq;
int counter=0;
///////////////////////////////////////////////////////////////////////////////
////////////////////////////Code Begining//////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
ttsbegin;
        try
        {
              changecompany('sdx')
              {
                while select invtm
                {
                    if(invtm)
                    {
                        changecompany('haq')
                        {
                            while select forupdate invtmhaq where invtmhaq.ItemId == invtm.ItemId
                            {
                                if(invtmhaq)
                                {
                                      invtmhaq.Price= invtm.Price;
                                      invtmhaq.update();
                                      counter++;
                                 }
                            }
                        }
                    }
                }
              }
       }
            catch
                {
                Error(strfmt("Can not apple to insert "));
                }
 ttscommit;
 info(strfmt("Number Of rows Inserted : %1",counter));

}

Comments

Vinayappan said…
table = null;
worked in my case.
If you are using changecompany(''); in a loop, table = null; should be used.

Popular Posts