Change Tracking Feature of CRM 2015 Online Update 1

By | May 26, 2015

As we have integration of Dynamics CRM with other external systems, in that case we need to keep track of changes that were done after last synchronization of data and we integrate only those changes in external system. To achieve this previously we have to add some kind of custom logic like add one date field in CRM to track the date of last synchronization. Now, in CRM 2015 update 1 we have a built-in functionality to achieve this, it is called Change Tracking.

Change Tracking is used in Dynamics CRM to keep the data synchronized in a better way by detecting what data has changed since the data was last synchronized.

To retrieve the changes for an entity, we need to enable the Change Tracking functionality for that entity.

This feature can be enabled by using the customization user interface (UI) or programmatically by setting the ChangeTrackingEnabled property to True.

ChangeTracking2

To achieve this functionality CRM 2015 has introduced a new request called RetrieveEntityChangesRequest.

When you execute this request for the very first time, then it returns all records for the entity and that data can be used to sync into any other external system. The message also returns a DataToken that we can use in the RetrieveEntityChangesRequest, so that when we execute this request the next time then it returns data for those changes that occurred since last execution of request.

If you want to get the changed data(created/updated/deleted) of Account since last execution of request then RetrieveEntityChangesRequest message is used to retrieve the data. After executing this request it will return the BusinessEntityChangesCollection of new/updated and deleted records. The new/updated records can be retrieved from NewOrUpdatedItem and deleted item can be retrieved from RemovedOrDeletedItem.

Following is the sample code for RetrieveEntityChangesRequest:

RetrieveEntityChangesRequest changeTracking = new RetrieveEntityChangesRequest();

//set the properties

changeTracking.Columns = new ColumnSet(true);

changeTracking.DataVersion = “471550!05/15/2015 10:08:13″;

changeTracking.EntityName = “account”;

changeTracking.PageInfo = new PagingInfo() { Count = 5000, PageNumber = 1, ReturnTotalRecordCount = false };

RetrieveEntityChangesResponse res = (RetrieveEntityChangesResponse)_service.Execute(changeTracking);

//get the token

string dataToken = res.EntityChanges.DataToken;

BusinessEntityChangesCollection busEntChanColl = res.EntityChanges.Changes;

for (int bizColl = 0; busEntChanColl.Count > bizColl; bizColl++)
{
if (busEntChanColl[bizColl].Type.ToString().ToLowerInvariant() == “neworupdated”)
{
NewOrUpdatedItem createUp =(NewOrUpdatedItem) busEntChanColl[bizColl];
Entity changedEnt = createUp.NewOrUpdatedEntity;
}
else if(busEntChanColl[bizColl].Type.ToString().ToLowerInvariant() == “removeordeleted”)
{
RemovedOrDeletedItem removeOrDelItem = (RemovedOrDeletedItem)busEntChanColl[bizColl];
EntityReference delItem = removeOrDelItem.RemovedItem;
}
}

If you doesn`t provide the DataVersion parameter for request then it will always return all records.

So to retrieve only records those are Created/Updated/Delete after the last execution of request then you have to provide the DataVersion parameter, DataToken returned by previous execution of request is what we have to pass as DataVersion new value for the next request.

For example: res.EntityChanges.DataToken in the above code snippet gives us the DataToken.

If you have the windows service/ windows application for integration of CRM with another system then you can store the DataToken value in app.config. If you used it in a plug-in or a workflow then it is better to create custom entity to store value of DataToken.

Leave a Reply