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

Announcements

News and Announcements icon
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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders

These are the community rock stars!

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

#1
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 54 Most Valuable Professional

#2
11manish Profile Picture

11manish 21

#3
AT-28040446-0 Profile Picture

AT-28040446-0 13

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans