I have this Checking Transaction Entity, there will be a lookup field on that "cse_vendor_name" (Vendor Master Entity) and "cse_expense_tracker", so every time I will select a cse_vendor_name it will auto-fill the field of cse_expense_tracker. I already trying this work code, but it doesn't,
function makeRequest(method, url) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.setRequestHeader("OData-MaxVersion", "4.0");
xhr.setRequestHeader("OData-Version", "4.0");
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
});
};
xhr.send();
});}
function setParentAccountbasedonPrimaryContact() {
var lookup1 = Xrm.Page.getAttribute("cse_vendor_name").getValue()[0].id;
var clientUrl = Xrm.Page.context.getClientUrl();
var query = clientUrl + "/api/data/v8.0/cse_vendormaster(" + lookup1.slice(1, -1) + ")?$select=_cse_expense_category_value";
makeRequest('GET', query)
.then(function (res) {
var res2 = JSON.parse(res);
var guid = res2._cse_expense_category_value;
var query2 = clientUrl + "/api/data/v8.0/cse_checkingaccounttransaction(" + guid + ")?$select=cse_vendor_name";
makeRequest('GET', query2)
.then(function (response) {
var res3 = JSON.parse(response);
var value = new Array();
value[0] = new Object();
value[0].id = guid;
value[0].name = res3.name;
value[0].entityType = "cse_checkingaccounttransaction";
Xrm.Page.getAttribute("cse_expense_category").setValue(value);
})
.catch(function (err) {
console.error('there was an error!', err.statusText);
});
})
.catch(function (err) {
console.error(' there was an error!', err.statusText);
});
}
So this is the answer to fill the lookup field whenever I selected a lookup field. The only problem in here this solution is, there's a probelm in the field that says "LookUp control error:cannot add item of typename= to the lookup control" so when I click save. it will go again to the lookup to select the data.
function makeRequest(method, url) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.setRequestHeader("OData-MaxVersion", "4.0");
xhr.setRequestHeader("OData-Version", "4.0");
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
});
};
xhr.send();
});
}
function setExpenseCategorybasedonVendor() {
var lookup1 = Xrm.Page.getAttribute("cse_vendor_name").getValue()[0].id;
var clientUrl = Xrm.Page.context.getClientUrl();
var query = clientUrl + "/api/data/v8.0/cse_vendormasters(" + lookup1.slice(1, -1) + ")?$select=_cse_expense_category_value";
makeRequest('GET', query)
.then(function (res) {
var res2 = JSON.parse(res);
var guid = res2._cse_expense_category_value;
var query2 = clientUrl + "/api/data/v8.0/cse_expensemasters(" + guid + ")?$select=cse_name";
makeRequest('GET', query2)
.then(function (response) {
var res3 = JSON.parse(response);
var value = new Array();
value[0] = new Object();
value[0].id = guid;
value[0].name = res3.cse_name;
value[0].entityType = "cse_expensemasters";
Xrm.Page.getAttribute("cse_expense_category").setValue(value);
})
.catch(function (err) {
console.error('there was an error!', err.statusText);
});
})
.catch(function (err) {
console.error(' there was an error!', err.statusText);
});
}