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

Announcements

News and Announcements icon
Community site session details

Community site session details

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

REST api call javascript issue

(0) ShareShare
ReportReport
Posted on by 2,512

Hi, recently, I met the issue that when I have a REST call, I didn't get the return value(checkApprovedShippingAddress), I assume it is related to async javascript, please advise. 

if(checkApprovedShippingAddress(formContext)){
  alert("1")
}


function checkApprovedShippingAddress(formContext){
debugger;
var currentId = formContext.data.entity.getId();
var query = "ont_shippingaddresses?$select=ont_name,ont_antigenscreeningprogram&$filter=statuscode eq 122290000 and _ont_antigenscreeningprogram_value eq  '" currentId "'"
callAPI(query,function(data){
if(data.value.length > 0){
return true
}
else
{return false}
})
}


function callAPI(query, callback) {
    var globalContext = Xrm.Utility.getGlobalContext();
    var serverUrl = globalContext.getClientUrl();
    //var ODataPath = serverUrl   "/XRMServices/2011/OrganizationData.svc";
    var ODataPath = serverUrl   "/api/data/v9.1/";

    var req = new XMLHttpRequest();
    req.open("GET", ODataPath   query, true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var result = JSON.parse(this.response);
                callback(result);
            } else {
                alert(this.statusText);
            }
        }
    };
    req.send();
}

I have the same question (0)
  • Verified answer
    Gary Cook Profile Picture
    180 on at

    Your calling method will complete before your Async method has completed, you need to do a call back from your async method

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
11manish Profile Picture

11manish 52

#2
Manoj - ManoVerse Profile Picture

Manoj - ManoVerse 43 Super User 2026 Season 1

#3
Ayesha Wajahat Profile Picture

Ayesha Wajahat 14

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans