Monday 31 October 2016

Alternate Keys in Dynamics CRM

Hi,

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", "gopinath@crm.com");
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", "gopinath@crm.com");
service.Create(entContact);

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

Gopinath

1 comment:

  1. Thank you for this information. I'm not a programmer but I was able to customize CRM Dynamics forms and create workflows and processes. One of the challenges I've had since we've started using CRM in 2013 was to actively link a custom field in another entity and I'm wondering if this could be used for that purpose or if you know how to do this? For example, I have a field on the Account form called *contact type* which is a multiple list Option type. I would like for the contact type information to carry over to the contact type field for each of the associated contacts. Any idea how to do this?

    ReplyDelete