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

Categories:
  • pavanmanideep Profile Picture
    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
    84,309 Moderator 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

Dynamics 365 Community Update – Sep 16th

Welcome to the next edition of the Community Platform Update. This is a weekly…

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

#1
Jahanvi Profile Picture

Jahanvi 106

#2
Jatin kumar "Mandlay" Profile Picture

Jatin kumar "Mandlay" 9

#3
AbhiShahi Profile Picture

AbhiShahi 5

Featured topics

Product updates

Dynamics 365 release plans