The following post explains how to create and publish codeunits as web services in order to create and update Items in Business Central.
The web services that will be shown below will additionally allow the creation and assignment of the following information:
- Item Unit Of Measure.
- Item Attribute.
- Item Identifier.
- Variants
- Dimensions
- Stock Keep units.
Lastly, the code has been adjusted to work with ErrorInfo and GetCollectedErrors in order to prevent the generation of an error in the processing of the Item from stopping the whole process and finally to be able to detail which Items were not processed so that they can be corrected.
Native tools used
Code
Item Unit Of Measure:
Item Attribute:
Dimensions:
Variants:
Item Identifier:
Stock keeping Unit:
Process Item:
Insert Item:
This process has the following decoration: [ErrorBehavior(ErrorBehavior::Collect)]
and allows us to collect multiple errors, the idea was to be able to collect the errors triggered by each failed validation in the Item processing.
Additionally, this process goes through each Json Array (each Item) and returns as output the number of elements processed correctly and the number of those not processed with their respective error messages.
Codeunit installation
Finally, I want to share something I learned recently, and it is how to automatically install the codeunit in the list of web services through an Installation/Upgrade codeunit.
Tests
Before running the tests, I want to share more details on how to use and consume a codeunit as web service in this previous post.
I also leave the official information from Microsoft.
VIDEO 1
Next, I leave the template used in Excel and in Json format.
Video 2
Conclusion
In this post, we saw how to massively create/update Items with related tables in Business Central through web services and also how to use GetCollectedErrors and ErrorInfo to queue the Items that would have errors but not stop the whole process. Of course, everything was an example, more fields and more validations could be extended.
I hope this will help,
All the code can be found in the Github link.