How to Obtain Different PDF Reports of Sales Orders through Webservices

Greetings to all!

In the world of ERP solutions, the capability to create reports and disseminate them across different systems is of paramount importance. This functionality becomes even more critical within platforms like Microsoft Dynamics 365 Business Central, celebrated for its robust reporting features.

Today, I will unveil a strategy that involves designing a web service in Business Central that is capable of generating a variety of PDF reports for sales orders.

Table of Contents

  1. Web Services Operation Demonstration in Postman
  2. AL Code Explanation
  3. Consuming the Web Services in Postman
  4. Conclusions

1. Web Services Operation Demonstration in Postman.

Video demo

2. AL Code Explanation.

To realize this, I have developed a codeunit called ‘ProcessReport’. This codeunit contains the essential procedure GetSalesOrderReportPDF, which takes a JSON object as an input. This JSON object should encompass three properties: ‘Document No’, ‘Document Type’, and ‘Report ID’.

Body Request

{
   "jsonText":"{\"Document No\":\"S-ORD101001\",\"Document Type\":\"1\",\"Report ID\":\"1302\"}"
}

Upon receiving the JSON object, GetSalesOrderReportPDF decodes the values and uses them to generate the designated sales order report in PDF format. Subsequently, the PDF report is converted into a base64 string, enabling straightforward data exchange across varying systems.

The GetSalesReportBase64 function, nestled within GetSalesOrderReportPDF, serves as the driving force behind the operation. It creates the PDF report based on the provided sales header, document type, document number, and report ID. The report is temporarily stored as a blob before being converted into a base64 string, which is subsequently encapsulated into a JSON object and returned.

3. Consuming the Web Services in Postman.

Creating the 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/ProcessReport_GetSalesOrderReportPDF?company='CRONUS%20USA%2C%20Inc.'

For more information on how to make calls to Webservices of the codeunit type, I share this link: How to create Sales Order through Postman in Business Central.

I also leave the official information from Microsoft.

Postman Image

4. Conclusions.

In conclusion, leveraging AL code and web services can create customizable solutions in the Dynamics 365 Business Central ecosystem. This post demonstrates generating diverse PDF sales order reports, then converting them into base64 strings for easy data exchange. With minor adjustments, this web service can return other document types.

Key to success includes accurate JSON input, efficient exception handling, and understanding code flow. This approach underscores Business Central’s adaptability and seamless interaction with other systems.

I hope this inspires you to innovate and enhance your Business Central usage. Stay tuned for more insights and tutorials on maximizing Dynamics 365 and AL programming!

CODE

You can get the whole project at this GitHub link.

I hope this has been helpful.

Leave a Reply

Your email address will not be published. Required fields are marked *