Dynamics 365 Education and Knowledge

Dynamics 365 Education and Knowledge

https://charlesabikhirs.blogspot.com

RETRIEVE RECORDS USING ALTERNATE KEYS IN DYNAMICS 365 SDK

Published

RETRIEVE RECORDS USING ALTERNATE KEYS IN DYNAMICS 365 SDK

With Dynamics 365 SDK, you can retrieve a single record by its GUID using the Retrieve method, but, what if you don't have the GUID of the record ?

Another approach is to retrieve the record by its alternative keys. However, you cannot use the Retrieve method to do this because it does not have any option to pass the alternate key value. This approach will need the RetrieveRequest to be done.

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

Below, is a sample code on how to retrieve a record based on alternate key in Dynamics 365 using the SDK.

  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 RetrieveRecordByKey()
    {
    var keys = new KeyAttributeCollection
    {
    { Account.Fields.Name, "test key" },
    };
    var request = new RetrieveRequest
    {
    ColumnSet = new ColumnSet(Account.Fields.Name),
    Target = new EntityReference(Account.EntityLogicalName, keys)
    };
    try
    {
    var response = (RetrieveResponse)AdminService.Execute(request);
    if (response.Entity != null)
    {
    //Do something here
    }
    }
    catch (Exception ex)
    {
    }
    }

    Alternate key 1

    Alternate key 2

  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