Set Lookup Value using JavaScript in MSCRM

Syntax:

Name Type Description
lookUpSchemaName String The lookup attribute logical name
entitySchemaName String The logical name of the entity being set.
recordId String A string representation of the GUID value for the record being set.
The expected format is “{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}”.
recordName String The text to be displayed in the lookup.
function setLookupValue(lookUpSchemaName, entitySchemaName, recordId, recordName) {
ย ย ย  var lookUpObj = [];
ย ย ย  lookUpObj[0] = {};
ย ย ย  lookUpObj[0].id = recordId;
ย ย ย  lookUpObj[0].entityType = entitySchemaName;
ย ย ย  lookUpObj[0].name = recordName;
ย ย ย  if (Xrm.Page.getAttribute(lookUpSchemaName) != null)
ย ย ย ย ย ย ย  Xrm.Page.getAttribute(lookUpSchemaName).setValue(lookUpObj);
}

Alternate Method

function setLookupValue(lookUpSchemaName, entitySchemaName, recordId, recordName) {
ย ย ย  Xrm.Page.getAttribute(lookUpSchemaName).setValue([{
ย ย ย ย ย ย ย  entityType: entitySchemaName,
ย ย ย ย ย ย ย  id: recordId,
ย ย ย ย ย ย ย  name: recordName
ย ย ย  }]);
}

Please provide your valuable comments on this article.


Discover more from Arun Potti's Power Platform blog

Subscribe to get the latest posts sent to your email.

Leave a Reply