Sylzz Heartnet Sylzz Heartnet - 9 months ago 25
Javascript Question

Javascript not Trigerring in Dynamic Crm on change

I have two entity,one of my entity triggered my java script, the other entity doesn't trigger it. They have both same fields(same name). I insert the js code in
the on change event of lookup field(both). I already checked the name but they both the same. So I typed scribble on the code, like "sfsf" to test if an error message will occur but nothing happen.Do you think what is the problem?

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() {
if(Xrm.Page.getAttribute("cse_vendor_name") && Xrm.Page.getAttribute("cse_vendor_name").getValue()){
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,cse_expensemasterid";
makeRequest('GET', query2)
.then(function (response) {
var res3 = JSON.parse(response);
var value = new Array();
value[0] = new Object();
value[0].id = res3.cse_expensemasterid;
value[0].name = res3.cse_name;
value[0].entityType = "cse_expensemasters";


if (lookup1 != null){

Xrm.Page.getAttribute("cse_expense_category").setValue(value);
Xrm.Page.getControl("cse_expense_category").setFocus();
Xrm.Page.getControl("cse_amount").setFocus();

}

else{
Xrm.Page.getAttribute("cse_expense_category").setValue(null);
Xrm.Page.getElementById("cse_expense_category").clear();
Xrm.Page.getElementById("cse_vendor_name").clear();
Xrm.Page.getControl("cse_othervendor").setFocus();

}

})
.catch(function (err) {
console.error('there was an error!', err.statusText);
});
})
.catch(function (err) {
console.error(' there was an error!', err.statusText);
});
}
}

Answer Source

I already know the answer in this, thank you