Different types of Events on form and events handlers using JavaScript with example Microsoft Dynamics 365 CE/CRM
CLICK HERE TO READ ABOVE BLOG
How to Set Lookup Field Value Using JavaScript -Dynamics 365
Three factors are required to set a lookup field’s value in MSCRM.
- GUID of the lookup record
- Name of the lookup record
- EntityName of the lookup entity
Below is the syntax used to set Value in Lookup field and we have given example as well
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
formContext.getAttribute("look Field Schema").setValue([{ id: "GUID", name: "Name Of Record", entityType: "EntityName" }]);
OR
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.
- JavaScript to set Account Name lookup value based on selected Customer Type.
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.