Saturday, 16 May 2015

Alternate key for CRM 2015 spring release

Have you ever wished that CRM had a composite key to reference the entity data? Guess what your wishes are now granted With Spring release, CRM now makes keys available for all entities.
You can create a composite key/alternate key for multiple columns like Account Name + Account suburb or any such combinations. So the question in many people's mind would be how does this help?
With this, you can now define a non-clustered index in the database. This will allow quick searches as now non-clustered index can be created for alternate keys and that means table scans will not be necessary and the records can be fetched a lot faster.

This can also be used to identify data imported into CRM from external system. Please note alternate key is not GUID. It is new way to allow external systems store CRM reference.

Here is the code example which uses alternate keys

string accountNumber = "12345";
Money NewCreditLimit = new Money(5000000);
string newPrimaryContact =;

using (OrganizationServiceProxy service = GetOrgService())
    Entity account = new Entity() { LogicalName = "account",
                                    KeyAttributes = { { "new_subcode", "FABRIKAM" },
                                                      { "new_subacctcode", accountNumber } }

    //update account
    account["creditlimit"] = NewCreditLimit;
    account["primarycontactid"] = new EntityReference("contact", "emailaddress1", newPrimaryContact)
    UpdateRequest request = new UpdateRequest();
    request.Target = account;
    UpdateResponse response = (UpdateResponse)service.Execute(request);

Further details on alternate keys available on following MSDN article:

Looking forward to more tightly integrated systems.