Get / Set Lookup field value using javascript in CRM

Get / Set Lookup field value using javascript in CRM

What is lookup field?

A Lookup field type represents the relationship attribute on the related entity.

Required Attributes of lookup fields

id : The GUID of the item. Required for set.

name : The name of the item to be displayed. Required for set.

entityType : The entity name of the item. Required for set.

How to get values of a lookup field?

function getLookupDetails()
 var entityName, entityId, entityLabel, lookupFieldObject;

// parentaccountid is the lookup field name that we try to reach its values
 lookupFieldObject ="parentaccountid");
 if (lookupFieldObject.getValue() != null) {
 entityId = lookupFieldObject.getValue()[0].id;
 entityName = lookupFieldObject.getValue()[0].entityType;
 entityLabel = lookupFieldObject.getValue()[0].name;

// Here you can use the id, name and entityType of the lookup
// Here also you can get the entity and the attribute values of it
// To do that please refer to the Note below !

How to set a lookup field value?

The function below sets new_teamid lookup field:

function setLookupField()

 var lookupData = new Array();
 var lookupItem = new Object();
 //Set the GUID = myTeamId;
 //Set the name = myTeamName;
 lookupItem.entityType = "team";
 lookupData[0] = lookupItem;

//If existing value is empty, then set new value
 var existingValue = Xrm.Page.getAttribute("new_teamid").getValue();
 if (existingValue == null)

 id: myTeamId,
 name: myTeamName,
 entityType: "team"
 } else


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s