Today I want to share a way to create a Sales Order in Business Central using Json and Postam and this time, using a codeunit published as SOAP which will be treated as a REST API. For this, I have relied on this link
To create documents, we are going to create a codeunit that will be published as web services, this will have one global procedures, called Input.
Method Input
The input method will allow us to read the data sent by Postman with the Json format of our Sales Order in a variable of type JSON Object.
Note: it is important that the input variable “jsonText” is called exactly the same as what we would call it in Postman for it to work.
Request URL
We will use the following URL format which would be:
https://api.businesscentral.dynamics.com/v2.0/{{TenantId}}/{{SandboxName}}/ODataV4/ Service_Name +'_'+ Method_Name + Company='Company Name'
We are left with something similar to the following:
https://api.businesscentral.dynamics.com/v2.0/{{TenantId}}/{{SandboxName}}/ODataV4/CreateSalesInvoice_Input?company='CRONUS%20USA%2C%20Inc.'
Request Authorization
To perform the authentication we have used Oauth2, in this link, there is a post on how to configure it.
Request body
In the request body, supply a JSON representation of a salesInvoices in following format object.
Within the json format we have an array called Lines, which will allow us to create multiple lines if desired.
Response
If successful, this method returns the 200 OK response code and a Json object with the number of the created Sales Order.
Example in Postman
Sales Order in Business Central
If we go to Business Central we can see the document:
Explaining the code
Now, let’s try to show how we did it and a little bit how the code works.
CreateSalesInvoice, calls 2 methods, InsertSalesHeader that will allow us to create the Sales Header and the other InsertSalesLines that will allow us to create the Sales Lines.
Method CreateSalesInvoice
The GetJsonToken procedure allows us to read the JSON, according to the corresponding key.
For example in the following line of code we want to extract ‘documentDate’:
DocumentDate := GetJsonToken(JO, 'documentDate').AsValue().AsDate();
When using GetJsonToken we obtain the JsonToken value, which will be necessary depending on the data type, apply the AsDate, AsCode, etc. procedure as necessary.
Method InsertSalesHeaders
Method InsertSalesLines
Finally, I share the complete code that allows us to generate our Sales Order.
All Code
I hope this has helped you.
Pingback: Business Central: How to get any Records in JSON format - Ivan Singleton
Pingback: Business Central: How to assign Serial and Lot No. from Warehouse Receipt using webserives - Ivan Singleton
Pingback: Business Central: How to manage Items via web services - Ivan Singleton