New CRM SDK Feature - Alternate Keys

16 April 2015
Daniel Cai

The latest Microsoft Dynamics CRM 2015 Update 1 release (v7.1) introduces a number of platform enhancements. Alternate Key is one of the coolest features available in this release. 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. 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. With the introduction of the Alternate Key feature in CRM 2015 Spring release, it makes our job much easier when writing data integration code.

Create CRM Alternate Keys using CRM UI

To create an alternate key in CRM UI, you would navigate to Settings - Customizations, and choose Customize the System.

Then you will be able to create new alternate keys under the specific entity's Keys section.

2015-04 CRM Alternate Key

You will then be able to define the Alternate Key.

2015-04 Create CRM Alternate Key

You would need to give a name in order to identify the key. There is a Display Name, and there is a logical Name which includes your customization prefix.

The alternate key could be made up by one or more CRM fields depending on your business requirement.

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.

2015-04 Create CRM Alternate Key

Note that only a small number of CRM fields are available to be selected as the key fields. Your selection is limited to the following three types of CRM fields.

  • String
  • Integer
  • Decimal

Unfortunately Date/Time, Lookup and OptionSet fields cannot be used as the key fields in this release, which could be something that you might want to use sometimes.

You can alternatively use SDK to create CRM Alternate Keys. You will be looking at CreateEntityKeyRequest message if you ever want to do so.

Use Alternate Key in Your Code

You can use CRM alternate key in the following two scenarios.

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

The following code snippet demonstrates its use for Update action.

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", "[email protected]");

    UpdateRequest request = new UpdateRequest() { Target = account };
    UpdateResponse response = (UpdateResponse)service.Execute(request);
}

Note that the new SDK introduced new constructors for both Entity and EntityReference classes which you can utilize to reference the key that you have created for the concerned entity. An alternative way is to use KeyAttributes property to set values for each key field.

using (var service = new OrganizationService(crmConnection))
{
    // Use alternate key (accountnumber) field to identify an account record
    Entity account = new Entity("account")
    {
        KeyAttributes = new KeyAttributeCollection
        {
            {"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")
    {
        KeyAttributes = new KeyAttributeCollection
        {
            {"emailaddress1", "[email protected]"}
        }
    };

    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).

A record with the specified key values does not exist in account entity

In my next blog post, I will show you how to use CRM Alternate Key for UpsertRequest.

A couple of final notes before we conclude this blog post.

Hope you have liked this feature as much I do.

Read the full New SDK Capabilities Blog Series:

Archive

November 2024 3 October 2024 1 September 2024 1 August 2024 2 July 2024 1 June 2024 1 May 2024 1 April 2024 2 March 2024 2 February 2024 2 January 2024 2 December 2023 1 November 2023 1 October 2023 2 August 2023 1 July 2023 2 June 2023 1 May 2023 2 April 2023 1 March 2023 1 February 2023 1 January 2023 2 December 2022 1 November 2022 2 October 2022 2 September 2022 2 August 2022 2 July 2022 3 June 2022 2 May 2022 2 April 2022 3 March 2022 2 February 2022 1 January 2022 2 December 2021 1 October 2021 1 September 2021 2 August 2021 2 July 2021 2 June 2021 1 May 2021 1 April 2021 2 March 2021 2 February 2021 2 January 2021 2 December 2020 2 November 2020 4 October 2020 1 September 2020 3 August 2020 2 July 2020 1 June 2020 2 May 2020 1 April 2020 1 March 2020 1 February 2020 1 January 2020 1 December 2019 1 November 2019 1 October 2019 1 May 2019 1 February 2019 1 December 2018 2 November 2018 1 October 2018 4 September 2018 1 August 2018 1 July 2018 1 June 2018 3 April 2018 3 March 2018 3 February 2018 3 January 2018 2 December 2017 1 April 2017 1 March 2017 7 December 2016 1 November 2016 2 October 2016 1 September 2016 4 August 2016 1 June 2016 1 May 2016 3 April 2016 1 August 2015 1 April 2015 10 August 2014 1 July 2014 1 June 2014 2 May 2014 2 February 2014 1 January 2014 2 October 2013 1 September 2013 2 August 2013 2 June 2013 5 May 2013 2 March 2013 1 February 2013 1 January 2013 1 December 2012 2 November 2012 2 September 2012 2 July 2012 1 May 2012 3 April 2012 2 March 2012 2 January 2012 1

Tags