Skip to main content

Notifications

Announcements

No record found.

Service | Customer Service, Contact Center, Fie...
Suggested answer

Unable to get response from ODATA, it always returns null

Posted on by 1,329

Hi Folks,

I was not able get response from Web API. Below is my code...Can suggest me if anything is missing in the below piece of code..I searched for any errors, but couldn't find thing.

var currentUserId = Xrm.Utility.getGlobalContext().userSettings.userId.substr(1, 36);
var userRoleResults = null;
var Buname = null;
var invokeUrl = Xrm.Utility.getGlobalContext().getClientUrl() + "/api/data/v9.1/systemusers(" + currentUserId + ")?$select=_businessunitid_value&$expand=businessunitid($select=name)";//,systemuserid&$expand=systemuserroles_association($select=name)
var req = new XMLHttpRequest();
req.open("GET", invokeUrl, true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json;charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0 ");
req.setRequestHeader("OData-Version", "4.0 ");

if (req.responseXML != null)
req.send();

req.onreadystatechange = function () {
if (this.readyState == 4) {
req.onreadystatechange = null;
if (this.status == 200) {
retrieved = JSON.parse(this.response);
Buname = retrieved.businessunitid.name;
//Buname = retrieved.results[0].BusinessUnitId.Name;
}
else {
var error = JSON.parse(this.response).error;
throw error;
}
//req.send();
}
};

 alert(Buname);

The alert seems to be always be Null.

Thank you.

  • pavanmanideep Profile Picture
    pavanmanideep 1,329 on at
    RE: Unable to get response from ODATA, it always returns null

    Hi Andrew,

    Thanks for your quick reply, I followed your recommendation and changed to False instead of true. Also alert placed at the above mentioned position.

    However the status seems to be undefined, so not even getting into the loop to show the alert..I have added the snap for  your reference.

    Please suggest or recommend way to verify and fix this.

    pastedimage1622309738913v2.png

    /resized-image/__size/320x240/__key/communityserver-discussions-components-files/763/pastedimage1622309708069v1.png

    Thank you.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Unable to get response from ODATA, it always returns null

    Hello,

    Line

    req.open("GET", invokeUrl, true);

    says to me that your call is async - developer.mozilla.org/.../open

    That means that your alert will be executed before you will get the data back.

    You have 2 ways out:

    1. Put alert just after you set Buname like:

    Buname = retrieved.businessunitid.name;

    alert(Buname);

    2. Use sync calls instead.

    Good luck.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans