I want to say why I am doing a post about a subject that most of this is already said in other posts by better authors. But I wanted to put together all information about OData Codeunits Web services from publishing to consuming with APiRest and talk about SOAP deprecation.
Deprecation
Some releases ago you only can access to BC Codeunit functions from outside with SOAP. But some time ago Microsoft began to talk about SOAP deprecation. Will Microsoft deprecate SOAP? Right now Microsoft consider the SOAP deprecation only for non-API pages WS. Deprecated Features in W1 - Business Central | Microsoft Docs . Originally I understood that all SOAP integration WS will be deprecated, so I hurried up to find an alternative to SOAP connection. Later we noticed this will not be that way, only SOAP deprecation is for pages connections. SOAP communication is complex, but is widely supported by the industry nowadays so I think his general deprecation is not appropriate.
But the mistake of thinking of general deprecation drives us to work about accessing Codeunit WS with APIRest and there were gains in the process because APIRest calls are far simpler than SOAP and is worth consider to use Odata even we still have SOAP available.
Making Codeunit and function
codeunit 69010 "
{
[ServiceEnabled]
[Scope('Cloud')]
procedure ItemStock(ItemNo: Code[20]; LocationFilter: text[50]): Decimal
var
Item: Record Item;
begin
Item."No." := ItemNo;
Item.SetFilter("Location Filter", LocationFilter);
Item.CalcFields(Inventory);
exit(item.Inventory);
end;
No comments all we know how to do this.
Getting URL
This is the most useful part of the post: when we publish the Codeunit web service, the BC web services page doesn´t show the OData URL. Instead, we have this link to a Microsoft Doc that explains this kind of access. But in my opinion is not easy to get the final URL with this information. So I want explain for further uses how the OData URL is.
This is the exposed SOAP URL:
https://api.businesscentral.dynamics.com/v2.0/e8da9i67-a837-5636-9404-4sjdhg98s7m2/Sandbox/WS/CRONUS%20ES/Codeunit/WSName?tenant=hsjdgf6476k8987654&aid=FIN
And this is the same URL service with OData:
https://api.businesscentral.dynamics.com/v2.0/e8da9i67-a837-5636-9404-4sjdhg98s7m2/Sandbox/ODataV4/ WSName_ItemStock?Company=CRONUS%20ES
We have a common root in both protocols:
https://api.businesscentral.dynamics.com/v2.0/e8da9i67-a837-5636-9404-4sjdhg98s7m2/Sandbox
But the OData URL follows this way:
- Literal ODataV4: ODataV4/ WSName_ItemStock?Company=CRONUS%20ES
- Service name: ODataV4/WSName_ItemStock?Company=CRONUS%20ES
- Method Name: ODataV4/ WSName_ItemStock?Company=CRONUS%20ES
- Company: ODataV4/ WSName_ItemStock?Company=CRONUS%20ES
Consuming WS
When the time of calling arrives, you can notice how simple is to call OData instead SOAP. This is an example of calling previously declared codeunit method.
var request = require("request");
auth = require('./ApiKey.json');
var transUrl = require('./BaseUri.json').URL + "/Sandbox/ODataV4/WSName_ItemStock?Company=CRONUS%20ES"
var data2 = {"itemNo":"1896-S","locationFilter":""}
request.post(
{ url: transUrl,
auth,
headers:{
content_type: 'application/json'},
body: JSON.stringify(data2)}
, function (err, response, body) {
console.log(body);
});
The Camel Case question
Other Authors advice us previously about parameters syntax: even if you declare them in AL with Pascal Case (example ItemNo), with first letter in uppercase, we have to use this parameter in calling in Camel case, with the begging of the word in lowercase:
var data2 = {"itemNo":"1896-S","locationFilter":""}
If you don´t know this, you can lose a lot of time with this error.
*This post is locked for comments