Monday, 31 October 2016

Alternate Keys in Dynamics CRM


One of the challenges we get in integrations when integrating external data with CRM is that in Order to update a record, we must know the GUID of the record.

Now with the introduction of alternate keys in CRM, we can use a unique value in the data set as a key for updating records as part of an integration.

For example, you have an external system that you want to integrate to your Account entity and system uses an email address as its key. You will have to define email address field as a alternate key on the Account entity.

Navigate to Customize the System -> Account Entity-> Keys -> New

select the field you want to use as a key (Emailaddress in this example), click Add. Give it a Display Name. Click Ok.

The system will then create a database index on that field to ensure fast querying and enforce the uniqueness of the values. Depending on how much data you have in your database, this could take a while. While this is processing, the key’s status will be either In Progress.
When the indexing operation is complete, the key’s status will change to Active.
You can use this key for Update and Upsert operations via the SDK. Here is the sample of Upsert Operation.

Entity entAccount = new Entity("account", "emailaddress1", "");
entAccount.Attributes["name"] = "My Account 1234";
UpsertRequest request = new UpsertRequest()
      Target = entAccount
UpsertResponse response = (UpsertResponse)service.Execute(request);

The alternate key can be used for Entity Reference as well, wherein instead of specifying GUID we can now use alternate key.

Entity entContact = new Entity("contact");
entContact.Attributes["lastname"] = "Alternate Key Contact";
entContact.Attributes["parentcustomerid"] = new EntityReference("account", "emailaddress1", "");

Points to remember
  • You can define up to five different keys for an entity
  • Currently alternate key can only be defined on field type – string, integer and decimal.
Hope this helps.

Happy CRM'ing


No comments:

Post a Comment