You may need to track changes in a table or data entity for your own data staging or logging purposes. AIF framework has a change tracking feature which uses SQL server stored procedures to track data changes in AX tables, which is also the standard change tracking used by the DMF (DIXF) framework to handle incremental push of data. Implementation is as easy as below. Create a data entity (or a Query) for your source AX table and initialize DB tracking on your data entity using the code below (As of writing, in D365 FO Spring 2018 release) :

class AIFChangeTrackingInit
{
    public static void main(Args _args)
    {
        AifChangeTrackingScope scope = 'ANDVisitScheduleTrackingScope';
        DictDataEntity dictEntity   = new DictDataEntity(tableNum(ANDVisitScheduleEntity));
        boolean		isSuccess = false;
        AIFSQLCDCENABLEDTABLES  ctEnabledTables;

        new AifChangeTrackingPermission().assert();
        aifchangetrackingconfiguration::initialize();
        isSuccess = AifChangeTrackingConfiguration::enableChangeTrackingForDataEntity(scope,dictEntity,true, AifChangeTrackingType::SqlChangeTracking);
        info(strFmt("Is success : %1", isSuccess));
    }
}

Check if the initialization is successful from the infolog message, then insert, update or delete records in your source table for testing :

CaptureWSTrack.PNG

To list the changes, run the job below :

class AIFChangeTrackingTest
{
    public static void main(Args _args)
    {     

        DictDataEntity dictEntity   = new DictDataEntity(tableNum(ANDVisitScheduleEntity));

        AifChangeTracking       aifChangeTracking;
        AifChangeTrackingTable  changeTrackingTable;

        new AifChangeTrackingPermission().assert();

        aifChangeTracking = AifChangeTracking::constructFromDataEntity(dictEntity, '', AifChangeTrackingType::SqlChangeTracking);
        changeTrackingTable = aifChangeTracking.getChanges(DateTimeUtil::utcNow());

        while select changeTrackingTable
        {
            info(strFmt("Changed record : %1, type : %2 ", changeTrackingTable.KeyField_RecId, "Insert/Update"));
        }

        changeTrackingTable = aifChangeTracking.getDeletes(DateTimeUtil::utcNow());

        while select changeTrackingTable
        {
            info(strFmt("Deleted record : %1, type : %2 ", changeTrackingTable.KeyField_RecId, "Delete"));
        }
    }

}

You will see the results of change tracking :

CaptureDbTRack.PNG

If you run the job once again, it will only show you the changes that are done after the previous retrieval process. This way it only picks up the incremental data updates, making it suitable for staging and data synchronization.

 

[adinserter block=”9″]

 

 

 

Leave a Reply