Friday, August 26, 2016

AX 2012 Copy Data between companies

I had this requirement from one of the customers who had 45 legal entities in the system. They maintained a certain setups and data which was expected to be consistent across the different legal entities in the system. Obviously doing is consistently across all the legal entities was quite time consuming and tedious.

Below is one of the code block used to copy Data Interval from one company to another

    CompanyInfo         companyList;
    LedgerPeriodCode    periodCodesCopy, periodCodesPaste;
    DataAreaName        sourceDataArea;
    ;


    sourceDataArea = curext();

    while select companyList
    where companyList.DataArea != sourceDataArea
    {
        changeCompany( companyList.DataArea )
        {
                periodCodesPaste.clear();
                periodCodesCopy.clear();

                periodCodesCopy.company( sourceDataArea );
                while select periodCodesCopy
                {
                    select periodCodesPaste
                    where periodCodesPaste.Code == periodCodesCopy.Code;

                    if ( !periodCodesPaste )
                    {
                        ttsBegin;
                        periodCodesPaste.data ( periodCodesCopy.data() ) ;
                        periodCodesPaste.insert();                       
                        ttsCommit;
                    }
                }

            }   //change company

        }   //companylist

    info( "Period codes are copied" );