Hi All,
I am trying to post JSON data to the plugin via ajax. I am getting an error with 400 error code and the exception message as :
An error occurred while validating input parameters: Microsoft.OData.ODataException: An unexpected 'StartObject' node was found for property named 'ShipmentDetails' when reading from the JSON reader. A 'PrimitiveValue' node was expected.
The code is
var data = { "IsCreateShipmentTask": true }; data.ShipmentDetails = PrepareShipmentData(); var jsonData = JSON.stringify(data); $.ajax({ url: organizationUrl + "/api/data/v9.1/" + query, type: 'POST', contentType: "application/json; charset=utf-8", async: false, datatype: "json", data: jsonData, beforeSend: function (XMLHttpRequest) { //Specifying this header ensures that the results will be returned as JSON. XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, success: function (responseText) { } });
If I modify the above JSON payload & make it as
data = { "IsCreateShipmentTask": true, "ShipmentDetails" : "some value" };
then it's working. But not with JSON objects.
Please help me out of this issue.
Thanks
*This post is locked for comments
Thanks a lot, Kokulan your solution worked out.
Hi siddharth
You are getting the error for the following reason in my view
When you post the JSON payload CRM is trying to convert the string in ShipmentDetails property to JSON object. It expects a simple type (string) but it found a complex type.
You could try the following and see if that helps to force CRM to not to convert the ShipmentDetails into an object
var ShipmentDataPrepped = PrepareShipmentData();
var ShipmentDataPreppedStringified = JSON.stringify(ShipmentDataPrepped );
You could try to find and replace all the double quotes with a single quote or some other character that won't be used in your shipment data. You could all try and replace the (colon) : with some other char and see if that allows to pass it through as a string. The outcome of this should be something like the following
data =
{
"IsCreateShipmentTask": true,
"ShipmentDetails" : "-'hello'_'world'-"
};
Once you passed the JSON object into your Action code, then you can do find and replace to get your original JSON back.
Let me know how it goes
Hope this helps
Hi Kokulan, The query variable has the action name :
var query = "new_FedExTracking";
The ajax post is successfully working with simple JSON obj, but with complex json, it's throwing 400.
Could you share whats in the query variable?
Hi David, The question has been asked in the correct forum. I am using Dynamics 365 CRM Online.
It looks like you're using Dynamics 365 Finance & Operations, so it would be best to post this in the Dynamics 365 Finance & Operations Forum.
The JSON payload on which I am getting error is :
{"IsCreateShipmentTask":true,"ShipmentDetails":{"ShipperName":"Microsoft","ShipperCompany":"TEST","ShipperPhone":"5435345345","ShipperStreet":["3454","Gees Miller Road"],"ShipperCity":"Conyers","ShipperStateCode":"GA","ShipperPCode":"30013","ShipperCountryCode":"US","RecipientName":"Fasg","RecipientCompany":"TEST1","RecipientPhone":"46456645654","RecipientStreet":["4545","Gees Miller Road"],"RecipientCity":"Conyers","RecipientStateCode":"GA","RecipientPCode":"30013","RecipientCountryCode":"US","RecipientIsResidential":true,"Currency":"USD","ShipmentServiceType":"Fedex 2day","IsCOD":true,"LineItems":[{"Height":"13","Length":"20","Width":"25","Weight":0.5,"WeightUnitInLB":true,"CODAmount":198,"DimensionUnitInCM":false,"CommodityPieces":2,"CommodityDescription":"Shares","CommodityUnitPrice":99,"CommodityCountryOFManufacture":"US","CommodityCustomsValue":0}]}}
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,219 Super User 2024 Season 2
Martin Dráb 230,056 Most Valuable Professional
nmaenpaa 101,156