CREATE OR UPDATE (UPSERT) RECORDS USING ALTERNATE KEYS IN DYNAMICS 365 SDK

In another post, we saw how to retrieve a record based on alternate key in Dynamics 365 using the SDK

In this post, we will see how to create or update (upsert) a record based on alternate key in Dynamics 365 using the SDK .

The below sample code can be used to achieve the UpdateRequest.

  1. For the sake of this post, the alternate key is set up on the Account Name (name) field of the account entity.

    public void UpsertRecordByKey()
    {
    KeyAttributeCollection keys = new KeyAttributeCollection
    {
    { Account.Fields.Name, "cak account" }
    };
    Account objAccount = new Account();
    objAccount.Name = "cak account";
    UpsertRequest request = new UpsertRequest()
    {
    Target = objAccount
    };
    try
    {
    UpsertResponse response = (UpsertResponse)AdminService.Execute(request);
    if (response.RecordCreated)
    {
    //Do something here
    }
    else
    {
    //Do something else here
    }
    }
    catch (Exception ex)
    {
    }
    }
  2. You will get exception if there is no record with the specified key value, or no key is added to the specified entity.
    The specified key attributes are not a defined key for the account entity

Hope This Helps!

Comments

  1. Hi, in the example above, you define the KeyAttributeCollection keys, but don't use them...

    ReplyDelete

Post a Comment

Popular posts from this blog

DYNAMICS 365 FILTER LOOKUP FIELD BASED ON N:N RELATIONSHIP

DYNAMICS 365 LEVEL UP BROWSER EXTENSION - PART 1 - FORMS

DYNAMICS 365 REFERENCE PANEL