#013 CODEUNIT API’s – ODATA V4 UNBOUND ACTIONS
One of the key recommendations to improve Web Services performance in Business Central is to use OData protocol instead of the deprecated and old-fashioned SOAP.
According to Microsoft documentationA, using OData protocol in a Web Service can be up to 10 times faster than using the SOAP protocol.
Publishing a Web Service using OData protocol is simple when an entity is provided (Page or Query). When a Codeunit is published as a Web Service, only the SOAP Url is automatically provided. However, it is possible to call a Codeunit published Web Service using OData protocol, turning it into an “API Codeunit”.
(A) Writing efficient Web Services
Step 1: Create your Web Service Codeunit
In this example it is created a Codeunit with method that returns a
Json array containing Customer’s data, according to a No. and Name filter:
Step 2: Publish your app and publish your Codeunit as a Web Service
Go to the Web Services page and insert a new record with:
1. Object Type = Codeunit;
2. Object ID = Your Codeunit Id;
3. Service Name = Type the service name (example: CustWS);
4. Published = Yes.
Step 3: Obtain the Url to call the Web Service
By definition, an OData base url in Business Central is composed as it follows:
http://<ServerName>:<ODataPort>/<ServerInstance>/ ODataV4/
The url is then followed by the service name and the company parameter:
<ServiceName>?company=<CompanyName>
To call an unbound action, in addition to the Service Name, we need to add the method to call, so this is the standard url:
http://<ServerName>:<ODataPort>/<ServerInstance>/ODataV4/<ServiceName_MethodName>?company=<CompanyName>
Example:
http://localhost:7163/BC190/ODataV4/CustWS_GetCustomer?company=3adc449e-8621-ec11-bb76-000d3a2993
Step 4: Test your Web Service
Using Postman, you can now test your Web Service2.