HI,
I need to implement a function to return a boolean value for validating status change. but I can't get the correct value because of the an asynchronous JS function. I implement the following:
I have a function called 'isStatusValid ' triggered when Status onchange event:
var isStatusValid = function () {
var valStatus = formContext.getAttribute(STATUS_REASON).getValue();
switch (valStatus) {
case STATUS_REASON_LIST.xxxx:
return Validatexxxx();
break;
case STATUS_REASON_LIST.CLOSED:
return ValidateClosed();
break;
default:
return true;
}
}
--------below ----ValidateClosed() function here---
var ValidateClosed = function () {
var blIsValid = false;
var checkRecordInEndState = allRecordInEndState();
checkRecordInEndState.then(function (result) {
return result;
})
return blIsValid ;
}
---- below -- allRecordInEndState() function here--------- an asynchronous API call
var allRecordInEndState = function () {
//use promise
return new Promise(function (resolve, reject)
{
var blResult = true;
var req = new XMLHttpRequest();
req.open("GET", srmClientURL + "/api/data/v" + util.getCurrentCRMVersion() + "/.................., true);
.........
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
blResult = (results.value.length <= 0);
resolve(blResult);
} else {
reject(this.statusText);
}
}
};
req.send();
});
}
---------------------------
but at end, the function ValidateClosed() alway returns wrong value. How to fix it?
Thanks for your help!