Code snippets for interacting with Dataverse’s lookup data type

The is a simple reference guide of sample code snippets for interacting with Dataverse’s lookup data type in both C#, JavaScript & Power Automate.

Lookup field data type

It’s a bit more complex than other field data types and is of type EntityReference.

In C#

Getting a lookup field

Entity ccDetails = crmSvc.RetrieveMultiple(new FetchExpression(enquiryXml)).Entities[0];

EntityReference ccDetailsEntityRef = ccDetails.ToEntityReference();
or
EntityReference ccDetailsEntityRef = ccDetails.GetAttributeValue<EntityReference>("cpl_carddetails");

Guid ccDetailsId = ccDetailsEntityRef.Id; //i.e. = {CB11AAA1-9CA1-EA11-A111-000A1A1A0B00}
String ccDetailsLogicalName = ccDetailsEntityRef.LogicalName; //i.e. = cpl_carddetails

Setting a lookup field

Guid customerId = Guid.NewGuid();
EntityReference customer = new EntityReference(“cpl_customer”, customerId);
or
ccDetails[“cpl_customer”] = new EntityReference(“cpl_customer”, customerId);

In JavaScript/TypeScript

Getting a lookup field

var customerEntityRef = formContext.getAttribute(“customerlookup”);
var contactid = customerEntityRef.getValue()[0].id;
var contactidname = customerEntityRef.getValue()[0].name;
var contacttype = customerEntityRef.getValue()[0].entityType;
e.g.
contactid = “{CB11AAA1-9CA1-EA11-A111-000A1A1A0B00}”
contactidname = “Company ABC”
contactidtype = “account”

This functionality could be placed into a TypeScript file as a common function

public static getLookupValue(executionContext: Xrm.Events.EventContext, field: string): any {
   const formContext = executionContext.getFormContext();
   const lookupValue = {
      id: formContext.getAttribute(field).getValue()[0].id,
      name: formContext.getAttribute(field).getValue()[0].name,
      entityType: formContext.getAttribute(field).getValue()[0].entityType,
   };
   return lookupValue;
}

Consider an example of having a ‘current expenses’ subgrid on a ‘personal credit application form. When a new ‘current expense’ is created, there is a lookup field (cpl_pca) on the expense form which links back to the credit application. This lookup can be used, for example, to retrieve the type of personal credit application it is, from the parent entity. The type of personal credit applicaiton could then be used to determine which fields should be shown/hidden on the ‘current expenses’ form.

var lookupEntityRef = formContext.getAttribute('cpl_pca');
var pcaId = lookupEntityRef.getValue()[0].id;
var pcaEntityType = lookupEntityRef.getValue()[0].entityType;
Xrm.WebApi.retrieveRecord(pcaEntityType, pcaId, '?$select=cpl_applicationSubType').then(...

Setting a lookup field

To set a lookup field, need to set 3 parameters. Creates an N:1 relationship with the other entity.

var entityReference = new Array();
entityReference[0] = new Object();
entityReference[0].id = Xrm.Utility.getGlobalContext().userSettings.userId;
entityReference[0].name = Xrm.Utility.getGlobalContext().userSettings.userName;
entityReference[0].entityType = 'systemuser';
formContext.getAttribute("cpl_requestedby").setValue(entityReference);

or

var entityReference = [
   {id: formContext.getUserId(), 
    name: formContext.getUserName(),
    entityType: 'systemuser'}];
formContext.getAttribute("cpl_requestedby").setValue(entityReference);

or if the field is a lookup in a entity e.g. the field cpl_orgunit. The object ‘result’ is retrieved via the retrieveRecord Web API call

var entityReference = [
   {id: result["_cpl_orgunit_value"],
    name: result["_cpl_orgunit_value@OData.Community.Display.V1.FormattedValue"],
    entityType: result["_cpl_orgunit_value@Microsoft.Dynamics.CRM.lookuplogicalname"]}];
formContext.getAttribute("cpl_parentorgunit").setValue(entityReference);

or if it’s a retrieve multiple entities e.g. the entity cpl_individual. The object ‘results’ is retrieved via the retrieveMultipleRecords Web API call

var entityReference = [
   {id: results["results.entities[0].cpl_individualid"],
    name:results["results.entities[0].cpl_name"],
    entityType: results["cpl_individual"]}];
formContext.getAttribute("cpl_approver").setValue(entityReference);

This functionality could be placed into a TypeScript file as a common function

public static setLookupValue(executionContext: Xrm.Events.EventContext, field: string, id: string, recordName: string, entityName: string): void {
   const formContext = executionContext.getFormContext();
   const lookupValue: any[] = [];
   lookupValue[0] = {
      id: id,
      entityType: entityName,
      name: recordName,
   };
   formContext.getAttribute(field).setValue(lookupValue);
}

In Power Automate

Setting a lookup field in Power Automate