Quick Tip D365 CE: How to create an Alternate Key Field using c#.
Views (589)
Introduction
This is a small blog on How to create an Alternate Key using C# code.
Prerequisites
- D365 Technical Knowledge
- 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
This was originally posted here.
*This post is locked for comments