Recently delving into client-side scripting, I have been tirelessly searching through Google and various communities for answers without success. Allow me to present my issue for a clearer understanding of the predicament.
I have established two custom entities called cts_agent and cts_cases. My goal is to automatically populate all fields in cts_cases that are read-only, with the exception of the agent id field (a whole number) which is linked to the cts_agent entity form.
If it were an entity reference field, I could use a query expression to retrieve details from the agents_form and populate the information in my cts_form. However, I need to write a JavaScript query that can take the agent id, fetch the details, and present them in a JSON format to populate the cts_cases form. This dynamic retrieval of guid value and auto-populating cts_cases with JSON data present two challenges that I am struggling to overcome. I have managed to code a static version:
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/cts_agents(00000000-0000-0000-0000-000000000000)?$select=cts_addressline1,cts_addressline2,cts_addressline3,cts_city,cts_country,cts_email,cts_fax,cts_mobilenumber,cts_name,cts_phonenumber,cts_state,cts_zipcode", 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);
var cts_addressline1 = result["cts_addressline1"];
var cts_addressline2 = result["cts_addressline2"];
var cts_addressline3 = result["cts_addressline3"];
var cts_city = result["cts_city"];
var cts_country = result["cts_country"];
var cts_email = result["cts_email"];
var cts_fax = result["cts_fax"];
var cts_mobilenumber = result["cts_mobilenumber"];
var cts_name = result["cts_name"];
var cts_phonenumber = result["cts_phonenumber"];
var cts_state = result["cts_state"];
var cts_zipcode = result["cts_zipcode"];
var cts_zipcode_formatted = result["Email"];
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();