Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Error 500 when calling CancelSalesOrder action using webapi javascript

Posted on by Microsoft Employee

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Error 500 when calling CancelSalesOrder action using webapi javascript

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

  • jlattimer Profile Picture
    jlattimer 24,558 on at
    RE: Error 500 when calling CancelSalesOrder action using webapi javascript

    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
    Community Member Microsoft Employee on at
    RE: Error 500 when calling CancelSalesOrder action using webapi javascript

    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?

  • Verified answer
    jlattimer Profile Picture
    jlattimer 24,558 on at
    RE: Error 500 when calling CancelSalesOrder action using webapi javascript

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


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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans