Saturday, October 23, 2021

Set Lookup Value in MS CRM 365 using JavaScript

 

While working on client-side customization using Java Script in MS CRM 365, it is very common requirement to set the Lookup field value which can be done using following code-

Here is the sample function which can be use to set the Lookup filed value-

function setLookupField(executionContext,fieldName, recordId,
recordName, entitySchemaName) {
    var formContext = executionContext.getFormContext();
    var lookupData = new Array();
    var lookupItem = new Object();
    lookupItem.id = recordId;
    lookupItem.name = recordName;
    lookupItem.entityType = entitySchemaName;
    lookupData[0] = lookupItem;
formContext.data.entity.attributes.get("parentcontactid").setValue(lookupData);

}

 

The code demonstrate itself that it is not enough to set the record id to the field, it is necessary to set record id, name and entityType then the value will reflect correctly in the field which is placed on the form.

 

Hope this will help.

No comments:

Post a Comment

QueryExpression vs. FetchXML in MS CRM with C#

Microsoft Dynamics CRM (Customer Relationship Management) is a powerful platform that helps organizations streamline their business processe...