Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Answered

REST api call javascript issue

Posted on by 2,397

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

Categories:
  • 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

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 16th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

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

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,349 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,212 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans