Skip to main content

Notifications

Announcements

No record found.

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,409

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();
}

  • Verified answer
    Gary Cook Profile Picture
    Gary Cook 180 on at
    RE: REST api call javascript issue

    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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,436 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans