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...
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

  • Verified answer
    H V Profile Picture
    H V 347 on at
    RE: REST API with JavaScript

    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();

  • Verified answer
    RE: REST API with JavaScript

    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
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: REST API with JavaScript

    Rupesh,

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

    req.send

    with line

    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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans