Change Tracking (change tracking) Dynamics 365 Customer Enagement in

I am a Microsoft Dynamics 365 & Power Platform aspects engineer Rayong, Microsoft Most Valuable Professional is the July 2015 to June 2018 for three consecutive years Dynamics CRM / Business Solutions aspects (Microsoft MVP), I welcome the attention of the public micro-channel number MSFTDynamics365erLuoYong, reply or 20,190,810 349 may facilitate access to this article, but you can get the latest information I Bowen issued in the first room, follow me!

Dynamics 365 CRM from the 2016 version adds a new message called RetrieveEntityChangesRequest, this news can monitor changes in the data since the last acquisition of the CRM. , You first need to enable the entity change tracking, refer to document change tracking is enabled to control data synchronization to do, it is actually very simple, select the entity to change the track properties are saved and can be released.

 

To test, I will prepare the following records:

 

code show as below:

using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Services;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.IO;
using System.ServiceModel;

namespace LuoYongLab
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var orgService = new OrganizationService(new CrmConnection("CRM"));
                string dataToken, changedType;
                List<Entity> initialrecords = new List<Entity>();
                RetrieveEntityChangesRequest request = new RetrieveEntityChangesRequest();
                request.EntityName = "ly_test";
                request.Columns = new ColumnSet("ly_name", "ly_alternatekey");
                request.PageInfo = new PagingInfo() { Count = 2, PageNumber = 1, ReturnTotalRecordCount = false };
                request.DataVersion = "565904!01/05/2016 06:57:22";
                Console.WriteLine("request.DataVersion = string.Empty;最初的同步,获取所有记录");
                while (true)
                {
                    RetrieveEntityChangesResponse response = (RetrieveEntityChangesResponse)orgService.Execute(request);
                    if (response.EntityChanges.Changes.Count >= 1)
                    {
                        foreach (var change in response.EntityChanges.Changes)
                        {
                            if (change.Type == ChangeType.NewOrUpdated)
                            {
                                NewOrUpdatedItem changedItem = (NewOrUpdatedItem)change;
                                Entity changedRecord = changedItem.NewOrUpdatedEntity;
                                if (changedRecord != null)
                                {
                                    string firstName = changedRecord.GetAttributeValue<string>("ly_name");
                                    changedType = changedItem.Type.ToString();
                                    Console.WriteLine("变更类型={0} 名称={1}", changedType, firstName);
                                }
                            }
                            else if (change.Type == ChangeType.RemoveOrDeleted)
                            {
                                RemovedOrDeletedItem removedItem = (RemovedOrDeletedItem)change;
                                EntityReference removedRecord = removedItem.RemovedItem;
                                if (removedRecord != null)
                                {
                                    Guid id = removedRecord.Id;
                                    changedType = removedItem.Type.ToString();
                                    Console.WriteLine("变更类型={0} Id={1}", changedType, id.ToString());
                                }
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine(" No record has been changed at! " ); 
                    } 
                    IF (! Response.EntityChanges.MoreRecords) 
                    { 
                        dataToken = response.EntityChanges.DataToken; 
                        Console.WriteLine ( " The acquisition of the acquired record is changed when = {0} DataToken " , dataToken);
                         the using (SW = File.AppendText the StreamWriter ( @ " C: \ luoyong.txt " )) 
                        { 
                            sw.WriteLine ( String .Format ( " acquired at the time of this change records acquired = {0} DataToken ", dataToken));
                        }
                        break;
                    }
                    request.PageInfo.PageNumber++;
                    request.PageInfo.PagingCookie = response.EntityChanges.PagingCookie;
                }
                Console.WriteLine("程序运行完成");
                Console.ReadKey();
            }
            catch (FaultException ex)
            {
                Console.WriteLine("程序出现异常:ex.Message=" + ex.Message);
                Console.WriteLine("ex.StackTrace=" + ex.StackTrace);
                Console.ReadKey();
            }
        }
    }
}

 

I run the code after shots show:

 

Then I will value request.DataVersion code changed to a value of 565,904 to get! After 01/05/2016 06:55:25, results are as follows, the result is correct.

 

Then I added a record, modify records, delete records to see the results. I modified the result is showing two records, a new record, delete the two records, one of which is the existence of previously been removed, another one is added and then removed after the results are correct. The conclusion is, this new time period and then deleted records will only show delete it, does not display once the new, so we want to and external system integration time, remove the other system records the time and do not be surprised if does not exist.

 

For more information, please refer to the documentation:  the Use Change Tracking the Data to the Synchronize with External Systems 

 

Guess you like

Origin www.cnblogs.com/luoyong0201/p/Dynamics_365_Cusotmer_Engagement_use_change_tracking_to_synchronize_data.html