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

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

REST api call javascript issue

(0) ShareShare
ReportReport
Posted on by 2,510

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)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: REST api call javascript issue

    Hello,

    Yes, it is related to async nature of the call. You should start using promice for async scenarios.

  • sdnd2000 Profile Picture
    2,510 on at
    RE: REST api call javascript issue

    Thanks, Andrew, do you have a sample about promise async scenarios

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: REST api call javascript issue

    Sure - developer.mozilla.org/.../Using_promises

  • Verified answer
    vishal bagadia_crm Profile Picture
    490 on at
    RE: REST api call javascript issue

    Hello,

    Its a nature of async process. 

    Please refer the below code to use the proper async logic without any issue. Under async process, if you need to perform some action that is dependent on the result, then always include the actions under response loop.

    Call Method- checkApprovedShippingAddress(formContext)
    
    
    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 callAPI(query) {
        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);
                    if(data.value.length > 0){
                    alert("1");
                    }
                    else
                    {
                    alert("2");
                    }
                } else {
                    alert(this.statusText);
                }
            }
        };
        req.send();
    }


    Please mark the answer as verified, if it works for you.

    Regards,
    CRM Developer

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

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 > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
#ManoVerse Profile Picture

#ManoVerse 101

#2
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 66 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans