I have an option set in Projects and Entity Items that share the same name, new_2vpnapplicable (fields have been double checked for syntax and var type) and duplicate value set. The script in question below, I borrowed from Rawish Kumar - https://community.dynamics.com/crm/b/passiondynamics/archive/2018/01/13/get-lookup-value-from-other-entity-and-set-it-on-the-form-using-web-api-in-microsoft-dynamics-crm.
The script below works until line 45. I verified that the Projects entity has focus and that the value of the field fieldValueToCopy is 100000000 (as expected). I hard coded this value in place of the var, fieldValueToCopy, to verify there was no trouble with the variable. The field new_2vpnapplicable in Projects is not being updated, though I can modify it manually.
line 45: Xrm.Page.getAttribute("new_2vpnapplicable").setValue(fieldValueToCopy);
******* Script start ******
function VPNApplicable_Opt() {
debugger;
var fieldValueToCopy = Xrm.Page.getAttribute("new_2vpnapplicable").getValue();
var lookupObj = Xrm.Page.getAttribute("objectid").getValue();
var newid = lookupObj[0].id.slice(1, -1); // you will get perfect id like "EDCJDKDJDKJDJDKJDJKD" here.
var req = new XMLHttpRequest();
/*
var primaryProjectId;
EntityReference primaryProjectId = new EntityReference ("msdyn_projects", newid);
console.log(primaryProjectId);
queueitem["primaryProjectId"] = primaryProjectId;
*/
// req.open("PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/msdyn_projects(" + newid + ")?$select=new_2vpnapplicable", true);
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/msdyn_projects(" + newid + ")?$select=new_2vpnapplicable", true);
/*
var url = Xrm.Page.context.getClientUrl() + "/api/data/v9.0/msdyn_projects(" + newid + ")?$select=new_2vpnapplicable";
console.log(url);
req.open("GET", url, 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.new_2vpnapplicable; //get the id of the field
var retrivedformatedvalue = result["new_2vpnapplicable@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 = "msdyn_projects";
Xrm.Page.getAttribute("new_2vpnapplicable").setValue(fieldValueToCopy); //set the lookup value finally
} else
alert("some textt!!!!!!") // optional
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}
***** End of script
Tracing the code, I can observed the first pass through, the value of this.readyState is not 4. After hitting req.send(), the code loops back a few times before this.readyState is equal to 4 and proceeds. Any ideas as to why the update on line 45 is not occurring?
*This post is locked for comments