Martin "Goshoom" Dráb – Dynamics AX, .NET and Everything

Menu

JSON-based custom service with parameters (AX 7)

Dynamics 365 for Operations deploys custom web services in two ways: as SOAP-based services and JSON-based services. AX developers are often familiar with SOAP services (which were used in AX 2012), but JSON-based ones are new to them.

One particular challenge is passing arguments to a service. Let’s say we have an Add operation, which can sum two numbers.

publicint Add(int a,int b){return a + b;
}

To call the service, we have to provide values for both arguments, a and b. Because the expected format is JSON (Java Script Object Notation), we have to provide a JSON string describing an object with two properties (a and b) and their values. This is it:

{ a:2, b:5}

Note that names of the properties are important – they must match parameter names in the X++ method.

Because building JSON strings by yourself can be cumbersome (especially with more complex parameters), a better approach is working with objects and leaving conversion to JSON to a serializer.

It’s clearly nothing difficult, just make sure that your properties in JSON exactly match parameter names. For example, if parameters of my Add operation were prefixed with underscore (as usual in X++), the JSON string would have to be { _a: 2, _b: 5 }.

Just wanted to thank you for this post, helped me quite a lot.
I use classes as request & response objects which complicates the matter a little bit.
But my easy workaround was to add the soap wsdl (via add web/service reference).
all my request & response objects are then available and I can serialize/deserialize easily.

I switched to JSON because I’m unable to get the SOAP webservice to work on a mono .Net (Xamarin.Android). Already posted my issue on several forums, still curious why this isn’t working properly.

Thanks Martin,
But I need your support in posting json array.
Because it gives this error:
“Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray.”
Are there a specific format , or JsonObject that contain the array as a parameter?
Your support is highly appreciated

In your above example, you are sending a single object of type contract.
So, what if we want to send many contracts objects in the same post request?
I tried to send json array like : [{ a : 2, b : 5 },{ a : 1, b : 3 },{ a : 9, b : 4 }]
But, the server respond with this error:
“Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray.”

Hi Martin,
I was able to follow your steps and i was able to get the Deserialized object back to AX.
But the challenge is inserting the the data from “DataContract Class object” into a table.
At the moment i can only read the 1st record. Which implies i need to do some kind of loop on the Data Contract.
Please assist.

Let me use AddContract from my blog post as an example.
An instance of AddContract above can hold only one set of values (‘a’ and ‘b’). If you need several instances, you need a collection. You would declare the parameter in AX as List, decorated the method with AifCollectionTypeAttribute, and you would use an array in JSON. In AX, you would use a ListEnumerator to iterate the collection.
This isn’t a good place for extended discussion, sharing code samples and so on. Please go to Dynamics 365 for Finance and Operations Forum if you need more help. You’ll also reach more people there, not just myself.