Hi all,
Could you please help me resolve the issue below?
Scenario:
MS Dynamics CRM 2016 (on premises)
Custom Entity with two lookup fields Opportunity and Account. The custom entity is related to the opportunity. I'm trying to set the Account Lookup value on the Form Load event.
Problem:
Although I'm not getting any error, the field is not being populated. Please see code below. Any help is greatly appreciated.
function SetLookup(query, callback) {
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + query, 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);
callback(result);
}
}
};
req.send();
}
function addParentAccountFilter() {
var opportunityObj = Xrm.Page.getAttribute("new_opportunityid");
if (opportunityObj == null)
return;
var id = opportunityObj.getValue()[0].id;
SetLookup("/api/data/v8.2/opportunities(" + id + ")?$expand=customerid_account($select=accountid,name)", function(result)
{
if (result.hasOwnProperty("customerid_account")) {
var accOptions = new Array();
accOptions[0] = new Object();
accOptions[0].id = result["customerid_account"]["accountid"];
accOptions[0].name = result["customerid_account"]["name"];
accOptions[0].entityType = "account";
Xrm.Page.getAttribute("new_accountid").setValue(accOptions);
}
});
}
function Form_onload() {
addParentAccountFilter();
}
*This post is locked for comments