web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

how to get a boolean value from an asynchronous JS function

(0) ShareShare
ReportReport
Posted on by 80

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!

I have the same question (0)
  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Hello, 

    As you know this is because synchronous call has been deprecated most of the browser.

    You can call your asynchronous method in the form onload or in different onchange methid which triggers earlier then your actual call and in the onsuccess set the result in different hidden field. If you do that you can directly get the hidden field value and do your operations. 

  • Jane88 Profile Picture
    80 on at

    Thanks Goutam!

    I will try what you suggested above.

    Thanks

    Jane

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    You can also implement promise in your code to get ool value from async call. See below articel.

    ajitpatra.com/.../

    Please mark my answer verified if i were helpful

  • Jane88 Profile Picture
    80 on at

    This is what my function allRecordInEndState() does (implemented promise). But the function ValidateClosed() which calls this function allRecordInEndState(), didn't get the correct value because the value returns too late (async).

    Thanks!

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    Yes, NOTE: If the promise does not resolve within 10 seconds, the rule will resolve with a false value.

    Please mark my answer verified if i were helpful

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

News and Announcements

Season of Giving Solutions is Here!

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Pallavi Phade Profile Picture

Pallavi Phade 98

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 79 Super User 2025 Season 2

#3
TAHER Mehdi Profile Picture

TAHER Mehdi 48

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans