Dynamics 365 Education and Knowledge

Dynamics 365 Education and Knowledge

https://charlesabikhirs.blogspot.com

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

Published

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!

Continue to website...

More from Dynamics 365 Education and Knowledge