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 :
Microsoft Dynamics CRM (Archived)

Error 500 when calling CancelSalesOrder action using webapi javascript

(0) ShareShare
ReportReport
Posted on by

I'm trying to cancel an order programmatically using the CancelSalesOrder action. I've tried every which way syntax but keep getting either a 400 (bad request) or 500 (server error).

Here is the syntax that gives me error 500, what am I doing wrong?

var parameters = {};
var orderclose = {};

orderclose.salesorderid = "4210B4C1-C1F3-4460-8615-1BE68FDFCC40";
orderclose["@odata.type"] = "Microsoft.Dynamics.CRM.salesorder";
parameters.OrderClose = orderclose;
parameters.Status = 100000017;

var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/CancelSalesOrder", 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.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 204) {
           
            //Success - No Return Data - Do Something
        } else {
           
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send(JSON.stringify(parameters));

As a secondary question why does the cancel order dialog (called by the Cancel button on the Order form ribbon) have a description and cancel date value that a user can provide? Where is description and cancel date stored? Why aren't these fields a part of the API? 

*This post is locked for comments

I have the same question (0)
  • Verified answer
    jlattimer Profile Picture
    24,562 on at

    Something like this:

    var parameters = {};
    var orderclose = {};
    orderclose["salesorderid@odata.bind"] = "/salesorders(DDD69AD1-B4FA-E611-8109-FC15B4286724)";
    orderclose["@odata.type"] = "Microsoft.Dynamics.CRM.orderclose";
    parameters.OrderClose = orderclose;
    parameters.Status = 4;
    
    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/CancelSalesOrder", 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.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 204) {
                //Success - No Return Data - Do Something
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send(JSON.stringify(parameters));


  • Community Member Profile Picture
    on at

    Thanks so much! That worked!

    I tried a syntax virtually identical to the one you responded with, with the only difference :

    orderclose["@odata.type"] = "Microsoft.Dynamics.CRM.orderclose";

    in my version was:

    orderclose["@odata.type"] = "Microsoft.Dynamics.CRM.salesorder";

    How were you able to determine that the type should be orderclose?

  • jlattimer Profile Picture
    24,562 on at

    In this case - we're creating the OrderClose record which is required to cancel/complete an Order. So in this request were creating that record, and in the line in question, specifying the type of entity were creating and passing into the Cancel request.  

  • Community Member Profile Picture
    on at

    If it's an OrderClose in every case of this action it seems weird to have to specify it. Thanks again for your help!

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans