Wednesday, October 7, 2020

CRM SDK Feature – Entity Change Tracking

 The aim of this topic is to make a clear understanding of what is Entity Change Tracking and how Entity Change Tracking works.

Many of us use AzureData Export Service in our daily business scenarios but we don’t know the logic behind Data Export Service.

I’m pretty much sure, we will get a clear understanding of Entity Change Tracking and DataExport Service and how they are related to each other.

Pre-Requisite -

  •        Make a Console Application (.Net Framework)
  •        Use RetrieveEntityChangesRequest SDK method

Business Scenario-

Let’s say we have a requirement that dictates to retrieve those records that are recently created or modified in CRM Entities.

In such case, where our CRM is the source, we use fetch xml query to retrieve records. But what about the records which are modified recently. In such case we typically compare modified on date which is not a reliable solution and may not perform well when there are too many records in the source system.

Change Tracking is the feature to outplay in these scenarios.

Change Tracking feature offers a reliable and efficient way to track Transactional Data Changes for CRM Entities.

In other words, Change Tracking is used in Dynamics CRM to keep data synchronized in an efficient way by detecting what data has changed since the last data was synchronized.

Detection is done by DataToken. DataToken is the unique string that we get in Response from RetrieveEntityChangesRequest.

Let’s do it practically. We will use only 1 entity for better understanding.

Step 1 – Make a Console Application (.Net Framework).

Step 2 – Make CRM service as below-


Step 3 – Add a function “ChangeTrackPull()” as below.

Step 4 – We will call it in our CRM service.

Step 5 – Now go to the Entity property and Enable Change Tracking. Save and Publish.

Step 6 – We will go back to the Console Application and start debugging.

Step 7 – Below is the DataToken.

Step 8 – Now again debug the Console Application and stay on “response” to see the record counts.

The record count is 0 during the 2nd pull. This means none of the record is newly created, updated or deleted. Let’s update any existing record.

Step 9 – Go the Entity and update any record. Again, debug the application.

We get 1 record count and that’s because we updated 1 record after previous pull.

The DataToken is also updated in the response that will be used in next pull of data. So, save it somewhere.

Now here is 1 more important point to learn. We get the following type of record after an initial pull.

  •         Newly created or Updated record
  •          Deleted record

Step 10 – Update a code to know the type of record we will get.

Step 11 – Again go back to the Entity and Create a New Record, then debug an application.

We get 1 record count. Let’s check the record type whether it is newly created/updated record or deleted record.

Step 12 – Jump to line 83 in Console Application.

It is the NewOrUpdated record. Let’s delete some records from the same entity and debug it again.

Before that copy the updated DataToken. We will use it in next pull i.e. after deleting some records.

Step 13 – Delete some records and debug.

Here we get 4 record counts.

Now we know, in above case we get change Type as RemoveorDeleted.

 For better understanding let’s check one by one whether they are updated or deleted records. We will check in the same way as we have checked in Step 12.

Note: The response will be EntityReference in case of deleted record.

Now I hope we get a clear understanding of Change Tracking. This same concept works while Replicating Data using Data Export Service in Azure.

Let's jump to Part – Azure Data Export Service

1 comment:

Popular Posts