The script was created using Rawish Kumars example. It fails with the general error "Internal Server Error." From debugger mode, "req:XMLHttpRequest: response" indicates the error is "Object reference not set to an instance of an object".
**** Begin of script
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$expand=msdyn_project_QueueItems($select=objectid)", true);
/*
var url = Xrm.Page.context.getClientUrl() + "/api/data/v9.0/msdyn_projects(" + newid + ")?$select=new_2vpnapplicable, projectid$expand=msdyn_project_QueueItems($select=objectid)";
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";
// console.log(value[0]);
Xrm.Page.get
Attribute("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
I tested the URL and got the desired results:
*** URL results
@odata.context "xxxxxxxxxxxxxxxx.crm.dynamics.com/.../v9.0$metadata#msdyn_projects(new_2vpnapplicable)/$entity"
@odata.etag "W/\"17715491\""
new_2vpnapplicable 100000001
msdyn_projectid "089cc620-95c8-e711-8119-e0071b6a5d41"
**** end link
*This post is locked for comments
I have the same question (0)