Search through blog..

Monday, October 22, 2012

Company or Legal entity (DataAreaID) in Dynamics Ax


Axapta has the option of using the same application for information from different Companies (or legal entities). Even though all of the data is kept on the same physical database, Axapta will determine which data is to be used for each company.
  • Axapta manages this by using the system field dataAreaId to define which company the data belongs to.
  • Company or a legal entity can be said as a record in the CompanyInfo table.
  • dataAreaId field will be found in each and every table which is used to store data create/updated from an Axapta form.
  • And whenever a new record is added, dataAreaId is automatically initialized to the current company id.
  • Whenever a user requests to view data from forms and reports or when the application fetches data using a select statement, Axapta will always return data from the current company, as a filter for dataAreaId is automatically added by the kernel.
  • All the created companies will be stored in System Table name DataArea.

The below are few areas where multiple companies is useful: 
  1. If we wish to train new staff, we can create a new ‘test’ company to perform various example at the same time the ‘live’ data is not damaged.
  2. You may wish to have a ‘planning’ or ‘budget’ company where you can prepare future business models.
    You may have more than one physical company that handles completely different business, but uses the same systems functionality. You can have separate information to support each company.

ChangeCompany:
What to do if we need to fetch data from more than one company, there is keyword changeCompany() which can be used to switch to the selected company. The selected company will be only changed within the changeCompany() code block and the starting company is automatically set afterwards.

Example(1):

static void DataDic_ChangeCompany(Args _args)
{
DataArea dataArea;
CustTable custTable;
;
while select dataArea //To select each company in the Application
{
if (!dataArea.isVirtual) //To ignore Virtual companies
{
info(strfmt("Company: %1", dataArea.id));
changeCompany(dataArea.id) //ChangeCompany START
{
custTable = null;
while select custTable where custTable.AccountNum <'4003'
{
info(strfmt("%1, %2", custTable.dataAreaId, custTable.accountNum));
}
} //ChangeCompany END
}
}
}


Output:



Notice that in code, CustTable is set equal null, this is because, the table variable must be reset otherwise data will be fetched from the default company.
ChangeCompany() should be used with precaution as you might end up modifying data in the wrong company.

 

No comments: