How to Get and Set a Lookup Field Using JavaScript in Dynamics 365

I was trying to get the value of Lookup field in the Phone Call entity but was not able to set the value in the field. There are so many blog post in the forums which explain to set the values in the lookup field but I don't think those posts are helpful for the beginners like me so when you try to set a lookup value using JavaScript in Microsoft Dynamics 365, the value you use has to be an array and the values in that array must be of type object. Within the object the required components are the id, logical name and the entity type.


Also Read - How to connect SharePoint using PHP


Retrieving a lookup field through JavaScript can be a little more difficult than expected, it is important to note that using business rules you can access the values within a lookup and set other fields with this value. Regardless, there are some cases where you might want to use JavaScript to get a lookup field value.

This is a simple example:

Suppose you are working on the Phone Call entity and there is lookup field name Call To -

then you have to write the JavaScript code like this - 


function getLookup(){
if (Xrm.Page.data.entity.attributes.get("to").getValue() != null){
var toByID = Xrm.Page.data.entity.attributes.get("to").getValue()[0].id;
var toByName = Xrm.Page.data.entity.attributes.get("to").getValue()[0].name;
var toByType = Xrm.Page.data.entity.attributes.get("to").getValue()[0].entityType;
}


}


To set the Lookup Value you will need to write a code like this - 

function getLookup(){
if (Xrm.Page.data.entity.attributes.get("to").getValue() != null){
var toByID = Xrm.Page.data.entity.attributes.get("to").getValue()[0].id;
var toByName = Xrm.Page.data.entity.attributes.get("to").getValue()[0].name;
var toByType = Xrm.Page.data.entity.attributes.get("to").getValue()[0].entityType;
}
// For the Lookup fields 
if (toByID != null && toByName != null){
parameters["to"] = [{id : toByID , name : toByName, entityType : toByType}];
}

}


If you want to get the lookup field value and set that value in new cloned record then you have to write the code like this - 

function getLookup(){
if (Xrm.Page.data.entity.attributes.get("to").getValue() != null){
var toByID = Xrm.Page.data.entity.attributes.get("to").getValue()[0].id;
var toByName = Xrm.Page.data.entity.attributes.get("to").getValue()[0].name;
var toByType = Xrm.Page.data.entity.attributes.get("to").getValue()[0].entityType;
}
// For the Lookup fields 
if (toByID != null && toByName != null){
parameters["to"] = [{id : toByID , name : toByName, entityType : toByType}];
}

//Get the parameters and Open the entity
Xrm.Utility.openEntityForm("phonecall", null, parameters)
}

Please let me know if you have any questions regarding this post. Please also check this post "How to Clone the record using JavaScript and Open that record in new window".



If you find this blog post helpful then Please share it with your friends on Social media. Thank you.

Comments