Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Service | Customer Service, Contact Center, Fie...
Suggested answer

Unable to get response from ODATA, it always returns null

(0) ShareShare
ReportReport
Posted on by 1,331

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
    1,331 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
    84,331 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

Ramesh Kumar – Community Spotlight

We are honored to recognize Ramesh Kumar as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Service | Customer Service, Contact Center, Field Service, Guides

#1
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 51 Most Valuable Professional

#2
Ramesh Kumar Profile Picture

Ramesh Kumar 42

#3
David Shaw_UK Profile Picture

David Shaw_UK 27

Featured topics

Product updates

Dynamics 365 release plans