Use RetrieveDuplicateRequest in Dynamics CRM

By | November 25, 2015

Introduction:

Recently we had a requirement where we had to create Contact records from data received from other System and if Duplicate record found then avoid creation of records based on Duplicate Detection rules specified in the System and get the List of the Duplicate Records found in the system.

The Duplicate Detection rules might be modified or removed whenever needed in future.

We can avoid creation of records using the CreateRequest but as we wanted to get the Duplicate records too we choose the option to use “RetrieveDuplicatesRequest”.

Walkthrough:

We have Published Duplicate Detection rules for Contact in the System by default created for First Name Last Name, Email address and business phone number as you can see below:duplicate detection ruleIn order to create Contacts using the Duplicate Detection rules we follow below steps:

  • Suppose in System we already have 2 contacts with First Name: Jo and Last Name: Smith
  • Now we received the same data [First Name: Jo, Last Name: Smith] to create Contact in CRM.

So now how we can avoid creation of this record and display the duplicate records information:

Create an Entity object of Contact with all the required attributes as per business requirement firstname and lastname as you can see below:

Entity contactRecord = new Entity("contact");

       contactRecord.Attributes["firstname"] = "Jo";

contactRecord.Attributes["lastname"] = "Smith ";

contactRecord.Attributes["emailaddress1"] = "jo@test.com";

After you have created an Entity object pass it to the RetrieveDuplicatesRequest as below:

var request = new RetrieveDuplicatesRequest

                {
//Entity Object to be searched with the values filled for the attributes to check
                    BusinessEntity = contactRecord,
                   
//Logical Name of the Entity to check Matching Entity
   MatchingEntityName = contactRecord.LogicalName,

                    
                };

                var response = (RetrieveDuplicatesResponse)_service.Execute(request);

This request retrieves all the Duplicate records satisfying the Published Duplicate detection rules specified in the system.

response.DuplicateCollection.Entities.Count” will give the Duplicate records count found in the system.

If Zero count found then no duplicate records are found in the System and we can simply create the record.

If greater than Zero count found then we can retrieve the Duplicate records from response.DuplicateCollection.

Conclusion:

The RetrieveDuplicatesRequest can be used for detecting duplicate records found using the Duplicate Detection Rules specified in the System.

You may also like to see : Bing Map license are already part of Maplytics.

Leave a Reply

Your email address will not be published. Required fields are marked *