Wednesday 29 March 2017

Copy data from one company to other companies using X++

Copy data from one company to other companies using X++

Overview:

The requirement is to copy BudgetExceltemplate table data from ‘02’ company to other companies. Before import in other companies must delete the dump data.

Code:

The below job will copy and create records from ‘02’ to other companies,

static void CopyDataacrossCmy(Args _args)
{
    //BudgetExcelTemplate is the customized table.
    // Need to copy BudgetExcelTemplate table data from '02' company to all the company
    // Change the table which you want to copy from based on your needs.

    BudgetExcelTemplate   BudgettableFrom, BudgettableTo, tabledelete;
    DataArea            dataareatbl;  

    // ProgressCounter
    #avifiles
    SysOperationProgress                 progressbar;
    int                                  totalRecords;
    #define.TITLE("Copying data to other companies...")
    ;

    select count(RecId) from BudgettableFrom;

    totalRecords = any2int(BudgettableFrom.RecId);
    progressbar  = SysOperationProgress::newGeneral(#aviUpdate, #TITLE, totalRecords);

    // Delete the dump data in other companies except the from company data.
    while select dataareatbl where dataareatbl.id != '02'
    {
         changecompany(dataareatbl.id)
         { 
             tabledelete.clear();           
            ttsBegin;
            delete_from tabledelete where tabledelete.dataAreaId == dataareatbl.id;
            ttsCommit;           
         }
    }   
    BudgettableFrom.clear();   
    // Copy data from 02 company to rest of the companies
    while select dataareatbl where dataareatbl.id != "02"
    {      
        while select BudgettableFrom
        {
            changecompany(dataareatbl.id)
            {
                BudgettableTo.clear();

                ttsbegin;
                buf2buf(BudgettableFrom, BudgettableTo);
                BudgettableTo.insert();
                ttscommit;
            }
            progressbar.incCount();
        }
       
        BudgettableFrom.clear();
    }

    progressbar.hide();

}

Happy coding!

1 comment: