Hi guys, I am new to CRM Dynamics . I have a requirement.
Lets assume that I have 2 related Entities : "Cat" and "Dog" and I have simple fields "Cat_Food" and "Dog_Food" on them respectively . I have a lookup field of Entity Dog on Entity Cat's Form named as "Lookup_Cat". Now as soon as a lookup value of Dog is selected , I want Cat's "Cat_Food" field to be populated with the (selected lookup)Dog's "Dog_Food" field's value using Javascript.
Now I have seen on many websites that CRM REST BUILDER was suggested. I had Used it but It is not populating and neither giving any error: Here's the code:
// JavaScript source code
function abc(executionContext)
{
var formContext = executionContext.getFormContext();
var new_dogfood;
var lookup = new Array();
lookup = formContext.getAttribute("Cat_Food").getValue();
if (lookup != null)
{
var name = lookup[0].name;
var id = lookup[0].id;
var entityType = lookup[0].entityType;
}
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/Dog(id)?$select=Dog_Food", false);
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.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(this.response);
new_dogfood = result["Dog_Food"];
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
formContext.getAttribute("Cat_Food").setValue(new_dogfood);
}
Any help would be appreciated.