Thursday, 18 June 2015

Data Synchronization through x++ code in Microsoft Dynamics AX 2012

Normally when Data Synchronization is throwing an error or if you want to synchronize the database through x++ code here it is: 
It is a job you can run:
  
static void forceDataSyncronization(Args _args)
    {
        Dictionary             dict;
        int                    idx, lastIdx, totalTables;
        TableId                tableId;
        Application            application;
        SysOperationProgress   progress;
        StackBase              errorStack;
        ErrorTxt               errorTxt;
        ;
        application = new Application();
        dict = new Dictionary();
        totalTables = dict.tableCnt();
        progress = new SysOperationProgress();
        progress.setTotal(totalTables);
        progress.setCaption("@SYS90206");
        errorStack = new StackBase(Types::String);
   
        lastIdx = 0;
   
        if (Box::okCancel("Syncronization the database, It may take a long time?", DialogButton::Cancel) == DialogButton::Ok)
        {
            try
            {
                for (idx = lastIdx+1; idx <= totalTables; idx++)
                {
                    tableId = dict.tableCnt2Id(idx);
                    progress.setText(dict.tableName(tableId));
                    lastIdx = idx;
                    application.dbSynchronize(tableId, false, true, false);
                    progress.incCount();
                }
            }
            catch (Exception::Error)
            {
                errorTxt = strFmt("Error in table '%1' (%2)", tableId, dict.tableName(tableId));
                errorStack.push(errorTxt);
                retry;
            }
            setPrefix("@SYS86407");
            errorTxt = errorStack.pop();
            while (errorTxt)
            {
                error(errorTxt);
                errorTxt = errorStack.pop();
            }
            info('Sychrnonization is now done.');
        }
        else
        {
            info('Syncronization cancelled');
        }
    }

No comments:

Post a Comment