Setting lookup value
var lookupValue = new Array();
lookupValue[0] = new Object();
lookupValue[0].id = "{3c67ee7a-1407-4975-937f-03d6a5aba8e4}"; // GUID of the lookup id
lookupValue[0].name = "Emma Stone"; // Name of the lookup
lookupValue[0].entityType = "contact"; //Entity Type of the lookup entity
Xrm.Page.getAttribute("FieldName").setValue(lookupValue); // You need to replace the lookup field Name..
Code language: JavaScript (javascript)
v9.0 and later
formContext.getAttribute("FieldName").setValue(lookupValue);
Code language: CSS (css)
More simple with one line of code
Setting lookup value with one line of code
Xrm.Page.getAttribute("FieldName").setValue([{ id: recordId, name: recordName, entityType: entityname }]);
Code language: CSS (css)
v9.0 and later
formContext.getAttribute("FieldName").setValue([{ id: recordId, name: recordName, entityType: entityname }]);
Code language: CSS (css)
Above is an example to using CRM js set lookup value. Hope this help you 🙂