RETRIEVE RECORDS USING ALTERNATE KEYS IN DYNAMICS 365
Charles Abi Khirs
3,569
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.
Below, is a sample code on how to retrieve a record based on alternate key in Dynamics 365 using the SDK.
- 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)
{
}
}
- 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!
This was originally posted here.
*This post is locked for comments