How to use JavaScript to fill parent lookup on form in Dynamics 365 CRM on-premise? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to use JavaScript to fill parent lookup on form in Dynamics 365 CRM on-premise? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Lets assume that I have 3 related Entities (PhoneCall-Account-Contact). For detailed information I must say in phonecall form I have a custom lookup field that related to account and another one that related to contact and last one again related to account which used for parent account. Now I want a solution that help me when I fill account field after that fill parent account lookup with correct data or if I fill contact lookup first of all fill account with correct data then fill parent account field with correct data. I search many ways but I couldn’t find any way to find correct parent account and fill my lookups even I use business rules but it can’t help me.

Now I have seen on many websites that CRM REST BUILDER was suggested. I had Used it but it couldn’t solve my problem.

Answer

What you need is, a Javascript function to trigger on change of first lookup to query necessary fields from parent record and fill it in current child record form. Read more

function fillParentAccount() {

var lookup= Xrm.Page.getAttribute("accountfieldname").getValue();  //you will get the id with exxtra double quotes or square brackets by doing get value hence you to make it readable by CRM , you must slice it. i have use the below method:
var newid = lookup[0].id.slice(1, -1);  // you will get perfect id like "EDCJDKDJDKJDJDKJDJKD" here.
var req = new XMLHttpRequest(); //once you have the id , you have frame to make a webapi GET call by proving the newid we got.

req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/accounts(" + newid + ")?$select=_parentaccountfieldname_value", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
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); // you will get the retrieved value in object we stored in result var.
var retrivedvalue= result._parentaccountfieldname_value; //get the id of the field
var retrivedformatedvalue= result["_parentaccountfieldname_value@OData.Community.Display.V1.FormattedValue"]; //get the formatted name of the field
if (retrivedvalue!= null) {
var value = new Array();
value[0] = new Object();
value[0].id = retrivedvalue;
value[0].name = retrivedformatedvalue;
value[0].entityType = "account";
Xrm.Page.getAttribute("parentaccountfield").setValue(value); //set the lookup value finally
}
else
alert("some textt!!!!!!") // optional
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();

Make sure to change the field names and accuracy of your customizations.

We are here to answer your question about How to use JavaScript to fill parent lookup on form in Dynamics 365 CRM on-premise? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji