All about alternate keys in Dynamics CRM

Retrieving metadata items by name is generally easier because you probably already have some reference to this item name in your code. The following table lists the alternate key properties for retrieving metadata items by name. The primary benefit of this feature is to provide an alternative way to identify a CRM record.

Traditionally we use CRM entity’s primary key field to identify a CRM record, which stores GUID values and this becomes a challenge when we try to integrate CRM data with third-party database or application systems, which might use some natural keys to identify a record. It provides  much easier way to code while integrating with 3rd party systems.

Metadata item Alternate Key Example
Entity LogicalName GET /api/data/v8.2/EntityDefinitions(LogicalName=’account’)
Attribute LogicalName GET /api/data/v8.2/EntityDefinitions(LogicalName=’account’)/Attributes(LogicalName=’emailaddress1′)
Relationship SchemaName GET /api/data/v8.2/RelationshipDefinitions(SchemaName=’Account_Tasks’)
Global Option Set Name GET /api/data/v8.2/GlobalOptionSetDefinitions(Name=’metric_goaltype’)

When creating an alternative key, CRM kicks off a system job that creates a db index at CRM database server side, the purpose of the db index is to enforce the uniqueness of key while offering an optimal query performance. Depending on the number of existing CRM records that you have for the concerned entity in CRM system, it could take a while to create the index. You can see the most recent status of the index creating job. Only the below three fields could be created as keys and over all we can create 5 keys for an entity.

  • String
  • Integer
  • Decimal

Alternate keys could be used in the two scenarios as below

  • Update
  • Upsert (a new capability introduced in CRM 2015 Spring release, which will be further discussed in a following post)

Let us see performing an update operation through code with alternate keys

using (var service = new OrganizationService(crmConnection))

{

// Use alternate key (accountnumber) field to identify an account record

Entity account = new Entity(“account”, “accountnumber”, “ACT-12345”);

// Set new credit limit;

account[“creditlimit”] = new Money(100000);

// Entity reference using alternate key (emailaddress1) on contact entity

account[“primarycontactid”] = new EntityReference(“contact”, “emailaddress1”, “joe@duo.com”);

UpdateRequest request = new UpdateRequest() { Target = account };

UpdateResponse response = (UpdateResponse)service.Execute(request);

}

In the case that a CRM record cannot be found using the provided key value, you will receive the following error exception (where account is the entity name).

References: Alternate keys from MSDN

Cheers,

PMDY


Discover more from ECELLORS CRM Blog

Subscribe to get the latest posts sent to your email.

Unknown's avatar

Author: Pavan Mani Deep Y

Passionate for Power Platform. A technology geek who loves sharing the leanings, quick tips and new features on Dynamics 365 & related tools, technologies. An Azure IOT and Quantum Computing enthusiast...

3 thoughts on “All about alternate keys in Dynamics CRM”

  1. Wow! This could be one particular of the most useful blogs We have ever arrive across on this subject. Actually Fantastic. I am also a specialist in this topic so I can understand your hard work.

    Like

Leave a reply to Tyron Koczela Cancel reply

Discover more from ECELLORS CRM Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading