Alternate Keys as an Alternative to Duplicate Detection Rules
Recently, I came across one scenario where I shouldn’t allow the duplicate record creation.
Duplicate detection has been around since the early versions of Dynamics CRM. In 2015, alternate keys were introduced to help identify a record based on a unique combination of attributes to perform an upsert request. An upsert is the action of inserting a record if it is new, or updating it if it already exists, based on the primary key identifier (https://msdn.microsoft.com/en-us/library/dn932135.aspx). Logically, this means whenever an alternate key is defined, any new records that are created with the same key combination will throw a duplicate exception.
Note: When using Alternate Keys in this manner, be sure that the combination of attributes selected truly represents a unique value.
Setting up an alternate key(Copy pasted from MS blog):
Please follow the below steps to create the alternate Key:
1. First, navigate to System -> Customizations -> Customize the System.
2. Expand the Entity.
3. Click Keys and then New.
4. Provide a Display Name.
5.Select multiple attributes to act as a Composite, Unique key.
6. Add the selected attributes. In this instance, we chose First Name, Last Name, and Mobile Phone to be the unique keys.
Note: Only the data types of Single Line of Text, Whole Number, and Decimal Number are available for use.
7. Click OK. An indexing System Job will now be created.
That’s it! Your Composite/Alternate Key has now been created!
Now, when you create a record with the same combination as another entry in the system, you will receive the following error message as below:
Note that alternate key creation can fail sometimes. Always check, after creating your key, whether the creation has been successful. If a duplicate already exists in your dataset, the key creation will fail. To check the status of a newly created key, in your solution, navigate to Entities | <entity> | Keys and ensure the Status column states Active. If the creation fails, it shows the status as Failed.