Introduction

This is a small blog on How to create an Alternate Key using C# code.

Prerequisites

  1. D365 Technical Knowledge
  2. Entity and Field must be present

Description

As per the official documentation, we need to create an EntityKeyMetadata request and add basic information.

Sample Code.

                    string entityName = "account";                     string fieldName = "apsmc_externalid";                     string alternateKeyName = "apsmc_ExternalKey";                     string alternateKeyDisplayName = "External Id";                     EntityKeyMetadata entityKeyMetadata = new EntityKeyMetadata()                     {                         KeyAttributes = new string[] { fieldName },                         SchemaName = alternateKeyName,                         DisplayName = new Label(alternateKeyDisplayName, 1033)                     };                     CreateEntityKeyRequest createEntityKey = new CreateEntityKeyRequest()                     {                         EntityName = entityName,                         EntityKey = entityKeyMetadata,                     };                     _client.Execute(createEntityKey);

References

https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/define-alternate-keys-entity