Although CRM provides a duplicate detection process that can be configured using the User Interface, many time when developing application for CRM, such as Import Manager or Synchronization application, you require to create your own duplicate detection rules, execute the duplicate detection process and retrieve the results of the duplicates from within your application.
The code samples below show how to create a Duplicate Detection Rule, execute the duplicate detection process and retrieve the results of the duplicate records.
To create a Duplicate Detection Rule, we use the DuplicateRule class, and provide a name for the rule and the entity name that will be used for the rule. In the example below, we create a rule that will check for duplicate email addresses in the contact record. After creating the rule, we need to specify the condition for the rule. The DuplicateRuleCondition class allows us to specify the attribute for the condition, and the type of rule that will be used. In the example below we use the emailaddress1 field and the base and matching attribute, and we set the match as exact match (an OptionSet value of 0).