web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :

Quick Tip D365 CE: How to create an Alternate Key Field using c#.

Mihir Maruti Kadam Profile Picture Mihir Maruti Kadam 702

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


This was originally posted here.

Comments

*This post is locked for comments