Retrieve Value from Lookup Field using Java Script
Hi Readers,
Step 1: Create a JavaScript as mentioned below.
Function Name: retrive_customer_lookup
Pass parameter in function to access exiting context(Data) on record.
cr601_customername replace with Lookup field Logical Name.
customername_fieldControl store GUID of Customer Name Lookup Field.
Step 2: Write down the code in java script.
function retrive_customer_lookup(executionContext)
{
debugger;
var formContext = executionContext.getFormContext();
var customername_fieldControl = formContext.getAttribute("cr601_customername").getValue()[0].id.slice(1, -1);//;
if (customername_fieldControl !=null && customername_fieldControl != undefined && customername_fieldControl != "")
{
var req = new XMLHttpRequest();
req.open("GET", Xrm.Utility.getGlobalContext().getClientUrl() + "/api/data/v9.2/cr601_customerdetails("+customername_fieldControl+")?$select=cr601_address,cr601_city,cr601_state&$expand=cr601_cr601_customersitedetails_CustomerName_cr($select=_cr601_customername_value)", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Prefer", "odata.include-annotations=*");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(this.response);
console.log(result);
// Columns
var cr601_address = result["cr601_address"]; // Text
var cr601_city = result["cr601_city"]; // Text
var cr601_state = result["cr601_state"]; // Text
if (cr601_address != null)
{
formContext.getAttribute("cr601_address").setValue(cr601_address);
}
if (cr601_city != null)
{
formContext.getAttribute("cr601_city").setValue(cr601_city);
}
if (cr601_state != null)
{
formContext.getAttribute("cr601_state").setValue(cr601_state);
}
}
else {
console.log(this.responseText);
}
}
};
req.send();
}
}
Step 2: Open the Forms as below.
Comments
Post a Comment