Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Duplicate Detection and Merging using C#, CRM SDK

(0) ShareShare
ReportReport
Posted on by 480

Hi Everyone,

I am trying to create a utility (windows app) which will detect duplicates based on an existing "Duplicate Detection Rule" in Dynamics CRM and then will merge the records based on most recent record (modified on) found.

I am able to get the duplicates programmatically but I don't know how to identify the duplicate pairs i.e. which one is duplicate of which record.

My Present Code Looks Like This:-

BulkDetectDuplicatesRequest request = new BulkDetectDuplicatesRequest()
            {
                JobName = "Accounts with same Address",
                Query = new QueryExpression()
                {
                    EntityName = "account",
                    ColumnSet = new ColumnSet(true)
                },
                RecurrencePattern = String.Empty,
                RecurrenceStartTime = DateTime.Now,
                ToRecipients = new Guid[0],
                CCRecipients = new Guid[0]
            };

            BulkDetectDuplicatesResponse _response = (BulkDetectDuplicatesResponse)service.Execute(request);

            //Guid jobid = Guid.Parse("8549a21f-5b74-e811-8159-e0071b67aca1");

            WaitForAsyncJobToFinish(_response.JobId, 150);


            QueryByAttribute query = new QueryByAttribute()
            {
                ColumnSet = new ColumnSet(true),
                EntityName = "duplicaterecord"
            };
            query.Attributes.Add("asyncoperationid");
            query.Values.Add(_response.JobId);
            //retrieve all the duplicates found by the system Job
            EntityCollection results = service.RetrieveMultiple(query);

Any Suggestions?

*This post is locked for comments

  • Jag S Profile Picture
    Jag S 210 on at
    RE: Duplicate Detection and Merging using C#, CRM SDK

    Hi All,

    Can we merge custom entities as well. We have participants registering for multiple studies , which creates duplicates and then only element different is the study name.

    I like to merge the duplicates into one contact but retain which studies the contact registered.

    I'm new to dynamics crm and any help would be highly appreciated.

    Thanks

    Jag

  • Rizwan Aarif Profile Picture
    Rizwan Aarif 480 on at
    RE: Duplicate Detection and Merging using C#, CRM SDK

    Hi Ravi,

    Sorry, I was running it on "DuplicateRecord" by mistake. I corrected it and ran on "baserecordid" (an entity reference) and it worked fine.

    Thank you for the help :)

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 Moderator on at
    RE: Duplicate Detection and Merging using C#, CRM SDK

    This is strange. I have built this utility in past using the same concept. You sure duplicate detection is enabled for Account. The message retrieveDuplicate is supported for Account entity. Check below-

    msdn.microsoft.com/.../gg328244.aspx

    Hope this helps.

  • Rizwan Aarif Profile Picture
    Rizwan Aarif 480 on at
    RE: Duplicate Detection and Merging using C#, CRM SDK

    As suggested by you, I am looping through each record found in "results" and executing a RetrieveDuplicateRequest but I am getting "Duplicate detection is not supported on this record type.'" error. I am using this for Account entity.

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 Moderator on at
    RE: Duplicate Detection and Merging using C#, CRM SDK

    Hi Rizwan,

    Once you got all the duplicate records, you need to iterate through each one and then again find the duplicates of that record using RetrieveDuplicate message. Once you find the duplicate of that record, iterate again and merge it with the main record.

    Do remember to check for for status = active for each record as the records may merge with other records during the complete process.

    Hope this helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans