Hello,
I'm trying to implement the following function for Cases/Incidents:
* When you select a Contact [primarycontactid], the Account [customerid] should be automatically set to the Contacts Account [parentcustomerid]
I've created a function to do this, but for some reason it won't let users actually save the record - instead it tells them to fill out customerid. I'm not really sure what the issue is here, any suggestions?
function onChange_ContactId() {
// set up for async request
var sourceFieldName = "parentcustomerid";
var targetFieldName = "customerid";
var sourceEntityName = "contact";
var targetEntityName = "account";
var id = _getId("primarycontactid");
if (id == null || id == undefined) return; // field was set to null, do nothing
// build async request
var request = new XMLHttpRequest();
var query = "?$select=_"+sourceFieldName+"_value";
var expansion = "";
var reqLine = Xrm.Page.context.getClientUrl()+"/api/data/v9.1/"+sourceEntityName+"s("+id.slice(1, -1)+")"+query+expansion; // concatenates request string
request.open("GET",reqLine, true);
request.setRequestHeader("OData-MaxVersion","4.0");
request.setRequestHeader("OData-Version","4.0");
request.setRequestHeader("Accept","application/json");
request.setRequestHeader("Content-Type","application/json; charset=utf-8");
request.setRequestHeader("Prefer", "odata.include-annotations=\"*\""); // adds FormattedValue to the result
request.onreadystatechange = function () {
if (this.readyState === 4) { // is ready
request.onreadystatechange = null;
if (this.status === 200) { // and successful
var result = JSON.parse(this.response); // parse JSON response
if (result["_"+sourceFieldName+"_value"] == null || result["_"+sourceFieldName+"_value"] == "") return; // ensure there's a result
var reference = [{ // built entityReference from result
"entityType":targetEntityName,
"id":result["_"+sourceFieldName+"_value"],
"name":result["_"+sourceFieldName+"_value@OData.Community.Display.V1.FormattedValue"]
},];
Xrm.Page.getAttribute(targetFieldName).setValue(reference); // apply result to targetFieldName
} else {
alert("attempted to retrieve "+sourceEntityName+"="+id+"\nstatus is "+this.status+"\nresponse is:\n"+this.response);
}
}
}
request.send(); // send async request
}
*This post is locked for comments
I have the same question (0)