web
You’re offline. This is a read only version of the page.
close
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.

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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.

  • pavanmanideep Profile Picture
    1,331 on at

    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.

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 > Service | Customer Service, Contact Center, Field Service, Guides

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 53 Super User 2025 Season 2

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 27 Most Valuable Professional

#3
Soundari Profile Picture

Soundari 15

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans