Skip to main content

Notifications

Announcements

No record found.

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

  • Verified answer
    vishal bagadia_crm Profile Picture
    vishal bagadia_crm 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

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

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

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

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

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,321 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.

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

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

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 228,625 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans