{"id":1291,"date":"2015-05-26T12:57:07","date_gmt":"2015-05-26T07:27:07","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=1291"},"modified":"2015-05-26T12:57:07","modified_gmt":"2015-05-26T07:27:07","slug":"change-tracking-feature-of-crm-2015-update-1","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2015\/05\/change-tracking-feature-of-crm-2015-update-1\/","title":{"rendered":"Change Tracking Feature of CRM 2015 Online Update 1"},"content":{"rendered":"<p>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 <strong>Change Tracking<\/strong>.<\/p>\n<p><strong>Change Tracking<\/strong> 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.<\/p>\n<p>To retrieve the changes for an entity, we need to enable the <strong>Change Tracking<\/strong> functionality for that entity.<\/p>\n<p>This feature can be enabled by using the customization user interface (UI) or programmatically by setting the\u00a0<span style=\"color: #000000;\"><a style=\"color: #000000;\" href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.xrm.sdk.metadata.entitymetadata.changetrackingenabled.aspx\"><strong>ChangeTrackingEnabled<\/strong><\/a><\/span>\u00a0property to\u00a0<strong>True<\/strong>.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2015\/05\/ChangeTracking2.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-1298\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2015\/05\/ChangeTracking2.png\" alt=\"ChangeTracking2\" width=\"724\" height=\"366\" \/><\/a><\/p>\n<p>To achieve this functionality <strong>CRM\u00a02015<\/strong> has introduced a new request called RetrieveEntityChangesRequest.<\/p>\n<p>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 <strong>DataToken<\/strong>\u00a0that 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.<\/p>\n<p>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.<\/p>\n<p>Following is the sample code for <strong>RetrieveEntityChangesRequest<\/strong>:<\/p>\n<p>RetrieveEntityChangesRequest changeTracking = new RetrieveEntityChangesRequest();<\/p>\n<p>\/\/set the properties<\/p>\n<p>changeTracking.Columns = new ColumnSet(true);<\/p>\n<p>changeTracking.DataVersion = &#8220;471550!05\/15\/2015 10:08:13&#8221;;<\/p>\n<p>changeTracking.EntityName = &#8220;account&#8221;;<\/p>\n<p>changeTracking.PageInfo = new PagingInfo() { Count = 5000, PageNumber = 1, ReturnTotalRecordCount = false };<\/p>\n<p>RetrieveEntityChangesResponse res = (RetrieveEntityChangesResponse)_service.Execute(changeTracking);<\/p>\n<p>\/\/get the token<\/p>\n<p>string dataToken = res.EntityChanges.DataToken;<\/p>\n<p>BusinessEntityChangesCollection busEntChanColl = res.EntityChanges.Changes;<\/p>\n<p>for (int bizColl = 0; busEntChanColl.Count &gt; bizColl; bizColl++)<br \/>\n{<br \/>\nif (busEntChanColl[bizColl].Type.ToString().ToLowerInvariant() == &#8220;neworupdated&#8221;)<br \/>\n{<br \/>\nNewOrUpdatedItem createUp =(NewOrUpdatedItem) busEntChanColl[bizColl];<br \/>\nEntity changedEnt = createUp.NewOrUpdatedEntity;<br \/>\n}<br \/>\nelse if(busEntChanColl[bizColl].Type.ToString().ToLowerInvariant() == &#8220;removeordeleted&#8221;)<br \/>\n{<br \/>\nRemovedOrDeletedItem removeOrDelItem = (RemovedOrDeletedItem)busEntChanColl[bizColl];<br \/>\nEntityReference delItem = removeOrDelItem.RemovedItem;<br \/>\n}<br \/>\n}<\/p>\n<p>If you doesn`t provide the <strong>DataVersion<\/strong> parameter for request then it will always return all records.<\/p>\n<p>So to retrieve only records those are Created\/Updated\/Delete after the last execution of request then you have to provide the <strong>DataVersion<\/strong> parameter, <strong>DataToken<\/strong> returned by previous execution of request is what we have to pass as <strong>DataVersion <\/strong>new value for the next request.<\/p>\n<p>For example: <strong>res.EntityChanges.DataToken<\/strong> in the above code snippet gives us the <strong>DataToken<\/strong>.<\/p>\n<p>If you have the windows service\/ windows application for integration of CRM with another system then you can store the <strong>DataToken<\/strong> value in <strong>app.config<\/strong>. If you used it in a plug-in or a workflow then it is better to create <strong>custom entity<\/strong> to store value of <strong>DataToken<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2015\/05\/change-tracking-feature-of-crm-2015-update-1\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15,23,24],"tags":[266,597],"class_list":["post-1291","post","type-post","status-publish","format-standard","hentry","category-development","category-dynamics-crm-2015-update-1","category-dynamics-crm-2016","tag-change-tracking","tag-dynamics-crm-2015"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/1291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=1291"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/1291\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=1291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=1291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=1291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}