Entity Change Tracking in MS CRM

Change Tracking is usefull when we are synching the data with external sources. Earlier it was very difficult to track or maintain the changes since last synchronized. With small piece of code and samll setting it will be very easy now.

By selecting, or deselecting, change tracking for specific entities you can reduce the load on your server resources and save processing time when extracting Dynamics 365 data and synchronizing it to an external store. You can enable change tracking for both system and custom entities


  1. Go to Customizations > Customize the System.
  2. Select an entity, and under Data Services, select the Change Tracking check box.














string token;

            // Initialize page number.
            int pageNumber = 1;
            List<Entity> initialrecords = new List<Entity>();

            // Retrieve records by using Change Tracking feature.
            RetrieveEntityChangesRequest request = new RetrieveEntityChangesRequest();
            request.EntityName = _customBooksEntityName.ToLower();
            request.Columns = new ColumnSet("sample_bookcode", "sample_name", "sample_author");
            request.PageInfo = new PagingInfo() { Count = 5000, PageNumber = 1, ReturnTotalRecordCount = false };


            // Initial Synchronization. Retrieves all records as well as token value.
            Console.WriteLine("Initial synchronization....retrieving all records.");
            while (true)
            {
                RetrieveEntityChangesResponse response = (RetrieveEntityChangesResponse)_serviceProxy.Execute(request);

                initialrecords.AddRange(response.EntityChanges.Changes.Select(x => (x as NewOrUpdatedItem).NewOrUpdatedEntity).ToArray());
                initialrecords.ForEach(x => Console.WriteLine("initial record id:{0}", x.Id));
                if (!response.EntityChanges.MoreRecords)
                {
                    // Store token for later query
                    token = response.EntityChanges.DataToken;
                    break;

                }
                // Increment the page number to retrieve the next page.
                request.PageInfo.PageNumber++;
                // Set the paging cookie to the paging cookie returned from current results.
                request.PageInfo.PagingCookie = response.EntityChanges.PagingCookie;
            }


We need to store token for later query. Below are the constraints of entity change.

  1. Only one entity will be tracked in retrieve changes. If retrieve changes is executed with no version / or token, the server will treat it as the system minimum version, returning all of the records as new. Deleted objects won’t be returned.
  2. Changes will be returned if the last token is within a default value of 90 days. If it is more than 90 days, the system will return all the records.
  3. If a client has a set of changes for an entity, say version 1, a record is created and deleted prior to the next query for changes, they will get the deleted item even if they didn’t have the item to begin with.
  4. Records are retrieved in the order determined by server side logic. Usually, the end user will always get all new or updated records first (sorted by version number) followed by deleted records.  If there are 3000 records created or updated and 2000 records deleted, Dynamics 365 returns a collection of 5000 records, which have the first 3000 entries comprised of new or updated records and the last 2000 entries for deleted records.
  5. If the new or updated item collection is greater than 5000, the user can page through the collection.

No comments:

Post a Comment

Featured Post

Improving MS CRM Performance

Performance on MS CRM is always a crucial thing and we may follow different ways to achieve the performance thing. Below is the one more a...