Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

how to get a boolean value from an asynchronous JS function

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!

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: how to get a boolean value from an asynchronous JS function

    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

  • Jane88 Profile Picture
    Jane88 80 on at
    RE: how to get a boolean value from an asynchronous JS function

    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
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: how to get a boolean value from an asynchronous JS function

    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
    Jane88 80 on at
    RE: how to get a boolean value from an asynchronous JS function

    Thanks Goutam!

    I will try what you suggested above.

    Thanks

    Jane

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: how to get a boolean value from an asynchronous JS function

    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. 

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

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans