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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Service | Customer Service, Contact Center, Fie...
Answered

REST API with JavaScript

(0) ShareShare
ReportReport
Posted on by 170

Hi All

I have a script which is generated from CRM REST Builder. It works perfectly in CRM REST Builder.

But its not working in dynamics 365 web resource JS. Its not going inside req.onreadystatechange = function() {

I am not getting error as well. Here is the code:

function accountOnSave() {
debugger;
var req = new XMLHttpRequest();req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/incidents(336DE157-0AB8-4FAE-8D75-3F38BC540A2C)?$select=statecode,statuscode", true);

req.setRequestHeader("OData-MaxVersion", "4.0");

req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

req.onreadystatechange = function() { //Its not going inside this

if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(this.response);
var statecode = result["statecode"];
var statecode_formatted = result["statecode@OData.Community.Display.V1.FormattedValue"];
var statuscode = result["statuscode"];
var stageName = Xrm.Page.data.process.getSelectedStage().getName().toString();
alert("stageName--->" +stageName);
alert("statuscode--->" +statuscode);
if(statuscode == 1){
Xrm.Page.data.process.setStatus("active");
}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send
}

Any help would be appreciated.

Regards

Rupesh

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

    Rupesh,

    I believe that main reason is mistake in the code - replace line

    req.send

    with line

    req.send();

  • Verified answer
    ZeeshanGhause Profile Picture
    on at

    Hi Partner!

    req.send is a function not member so use this req.send(); instead req.send

    please Mark my Answer as Verified if it is.

    Thanks

  • Verified answer
    H V Profile Picture
    351 on at

    Hi,

    You need to encode url before passing. Please find updated code below:

    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl()   "/api/data/v9.1/incidents(c1dbdce0-ed7e-ea11-a811-000d3a300e5b)?$select=statecode,statuscode", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    
    req.onreadystatechange = function() { 
    
    if (this.readyState === 4) {
    req.onreadystatechange = null;
    if (this.status === 200) {
    var result = JSON.parse(this.response);
    var statecode = result["statecode"];
    var statecode_formatted = result["statecode@OData.Community.Display.V1.FormattedValue"];
    var statuscode = result["statuscode"];
    
    alert("statuscode--->"  statuscode);
    if(statuscode == 1){
    Xrm.Page.data.process.setStatus("active");
    }
    } else {
    Xrm.Utility.alertDialog(this.statusText);
    }
    }
    };
    req.send();

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

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

#1
11manish Profile Picture

11manish 36

#2
Mallesh Deshapaga Profile Picture

Mallesh Deshapaga 32

#3
Goloknath Profile Picture

Goloknath 28 User Group Leader

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans