Technology. People. Progress.
Development

Set a Dynamics 365 lookup value with JavaScript

Use the record ID, name and entity name to set a lookup field.

Set a Dynamics 365 lookup value with JavaScript

Different types of Events on form and events handlers using JavaScript with example Microsoft Dynamics 365 CE/CRM

CLICK HERE TO READ ABOVE BLOG

Call Xrm.WebApi.retrieve Record using JavaScript MSCRM/CE , Retrieving lookup / related entity using Web API JavaScript FetchXML Microsoft Dynamics CE / CRM

CLICK HERE TO READ ABOVE BLOG

How to Set Lookup Field Value Using JavaScript -Dynamics 365

Screenshot from the DVMSKE tutorial
How to Set Lookup Field Value Using JavaScript -Dynamics 365 — screenshot 1 View full size

Three factors are required to set a lookup field’s value in MSCRM.

  1. GUID of the lookup record
  2. Name of the lookup record
  3. EntityName of the lookup entity

Below is the syntax used to set Value in Lookup field  and we have given example as well

Code example
var lookupFieldValue = new Array();
lookupFieldValue[0] = new Object();
lookupFieldValue[0].id = GUID; // {567564ed-64c5-4666-ac66-0a3071c427e4}
lookupFieldValue[0].name = Name; //dvmske pvt ltd
loopupFieldValue[0].entity = EntityName; //account
formContext.getAttribute("look Field Schema").setValue(lookupFieldValue);

OR

Code example
formContext.getAttribute("look Field Schema").setValue([{ id: "GUID", name: "Name Of Record", entityType: "EntityName" }]);

OR

Code example
var lookupFieldValue = [];
lookupFieldValue[0] = {};
lookupFieldValue[0].id = GUID; // {567564ed-64c5-4666-ac66-0a3071c427e4}
lookupFieldValue[0].name = Name; //dvmske pvt ltd
loopupFieldValue[0].entity = EntityName; //account
formContext.getAttribute("look Field Schema").setValue(lookupFieldValue);

Scenario : On Select of Optionset Value , Auto set Lookup field

  • In Contact Entity, there are 2 fields i.e. “Account Name”  = Lookup of account & “Company Type” = Option set Field.
  • When User Select Company Type Option value, based on Company Type, Account name Lookup will be set automatically as Below JavaScript example.

Scenario : On Select of Optionset Value , Auto set Lookup field — screenshot 2
Scenario : On Select of Optionset Value , Auto set Lookup field — screenshot 2 View full size

  • JavaScript to set Account Name lookup value based on selected Customer Type.
Code example
function lookupValue(executionContext) {
var formContext = executionContext.getFormContext();
var companytype = formContext.getAttribute("dvmske_companytype").getValue();
if (companytype != null)
{
if
(companytype == 706880002) {
formContext.getAttribute("parentcustomerid").setValue([{ id: "042d0c62-06fe-ed11-8f6d-6045bda569ef", name: "Dvmske pvt ltd.", entityType: "account" }]);
}
else if (companytype == 706880000) {
formContext.getAttribute("parentcustomerid").setValue([{ id: "598bf867-d69e-eb11-b1ac-000d3a1343ea", name: "Adatum Corporation", entityType: "account" }]);
}
else if (companytype == 706880001) {
formContext.getAttribute("parentcustomerid").setValue([{ id: "0321d0f9-77e2-ed11-8848-6045bda569ef", name: "DefaultAccount-IOMLabOrderIntakeProvider", entityType: "account" }]);
}
}
else {
formContext.getAttribute("parentcustomerid").setValue([{ id: "042d0c62-06fe-ed11-8f6d-6045bda569ef", name: "Dvmske pvt ltd.", entityType: "account" }]);
}
}
  • We have added JavaScript “on save” Even of form.
  • When we select a option from Customer Type field i.e. select “Logistic Provider”, We can see lookup field will be populated automatically.

Thanks….

Your comments will appreciated.

Need help applying this to your business?

Talk to our team about your systems and next step.

Talk to our team
Open original image