Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

SDK.REST.createRecord SalesOrderDetail return error 400 The request should be a valid top-level resource object V8.2 on premises

Posted on by 50

hi ,

I have a html web resource where I need to generate a order and add items to order.  I can create  a order but when I tried to add order items it fails. I tried with ajax XMLHttpRequest , SDK.REST.createRecord 

both failed . 

 

var orderitem = {};                
                orderitem.IsProductOverridden = false;             
                orderitem.ProductId = { Id: "d270cd0e-a589-e811-80e2-005056be003c", LogicalName: "product" }; 
                orderitem.SalesOrderId = { Id: "07A5E897-B2D9-EB11-8111-0050569DDB30", LogicalName: "salesorder" }; 
                orderitem.UoMId = { Id: "455469c8-601f-e211-9c7d-005056be0040", LogicalName: "uom" }; 
                orderitem.Quantity = { Value: "1" };
                orderitem.IsPriceOverridden = true;
                orderitem.PricePerUnit = { Value:  "349.00"  } ;
                SDK.REST.createRecord(JSON.stringify(orderitem),  "SalesOrderDetail",
                    function success(result) {
                        console.log("created"   result.Id);                         
                    },
                    function (error) {
                        console.log(error.message);
                         
                    }
                );

this returns "400: Bad Request: Error processing request stream. The request should be a valid top-level resource object " ... the Ids I am passing definitely in the system. ans i have tried the same code with passing Name values of the entity references as well. but no luck. I have tried to add write-in order items , that also failed. 

I also tried to use Instance Web API  xxxxxxxxxxxx/.../ didn't work either.  I tired parent.Xrm.WebAPI  but that returns error undefined  and I saw Xrm.webAPI not enable in V8

var newProduct = {
                    "salesorderid@odata.bind": "/salesorders(07A5E897-B2D9-EB11-8111-0050569DDB30)",
                    "isproductoverridden": false,
                    "productid@odata.bind": "/products(d270cd0e-a589-e811-80e2-005056be003c)",
                    "uomid@odata.bind": "/uoms(455469c8-601f-e211-9c7d-005056be0040)",
                    "quantity": parseInt(1),
                    "ispriceoverridden": true,
                    "priceperunit": Number(parseFloat(349).toFixed(2))
                };


                var req = new XMLHttpRequest();
                req.open("POST", "https://xxxxxxxx/api/data/v8.2/salesorderdetails", 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");
                req.onreadystatechange = function () {
                    if (this.readyState == 4 /* complete */) {
                        req.onreadystatechange = null;
                        if (this.status == 200) {
                            console.log("success");
                        } else {
                            var error = JSON.parse(this.response).error;
                             console.log(error.message);
                        }
                    }
                };
                req.send(JSON.stringify(newProduct));

Could any one help me to resolve this please. I can create orders and order items using server side c#  scripting using a custom workflow activity  , but I want to approach this from html web resource and  need crm user to input few things to pass to order items which cannot be done via workflow.  looking forward experts answers.

thank you 

  • Wahaj Rashid Profile Picture
    Wahaj Rashid 11,319 on at
    RE: SDK.REST.createRecord SalesOrderDetail return error 400 The request should be a valid top-level resource object V8.2 on premises

    Hi,

    Are you sure this error is cause by the code to create Sales Order.

    This crash report is from a web resource, please debug the web resource code.

    Furthermore, if this problem is solved (creating order), please marke the approriate answer as verified (click 'Yes' under 'Did this answer your question?').

    Also, it is recommened to please open a seperate thread for a different issue.

  • Path1n1 Profile Picture
    Path1n1 50 on at
    RE: SDK.REST.createRecord SalesOrderDetail return error 400 The request should be a valid top-level resource object V8.2 on premises

    hi, I have retry my second code using data/api , i found out it works and create the order item but return crash report in debug mode. but there is no errors in script .

    CrashReport:
    		FileName:
    		LineNumber:0
    		Function:
    		ErrorReport:
      1.0
      
       Script error.
       0
       
       /{637610856290000019}/WebResources/my_webresourcename
       
       
       
       
      
      
       Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.64
       undefined
       undefined
       undefined
       1920x1080
       Web
       Online
       2021-07-05T13:42:35
      
    

    any idea 

  • Suggested answer
    Wahaj Rashid Profile Picture
    Wahaj Rashid 11,319 on at
    RE: SDK.REST.createRecord SalesOrderDetail return error 400 The request should be a valid top-level resource object V8.2 on premises

    Hi,

    Thank you for your query.

    Did you try using bind with sales order, for example, to set sales order id:

    orderitem["salesorderid@odata.bind"] = "/salesorders(<id>)" 

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans