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\"}" }
codeunit 59700 ProcessReport { Access = Public; // A public function which always returns the string 'Pong' procedure Ping(): Text begin exit('Pong'); end; // Function to generate a PDF report of a sales order. // The input is a JSON text containing the document number, document type, and report ID. // It returns the PDF report in base64 format. procedure GetSalesOrderReportPDF(jsonText: Text) ReportBase64: Text var SalesHeader: Record "Sales Header"; JSONManagement: Codeunit "JSON Management"; SalesDocumentType: enum "Sales Document Type"; ValueText: Text; DocumentNo: Code[20]; ReportID: Integer; begin // Initialize JSON object from the given string JSONManagement.InitializeObject(jsonText); // Parse document number from the JSON JSONManagement.GetStringPropertyValueByName('Document No', ValueText); DocumentNo := CopyStr(ValueText.ToUpper(), 1, MaxStrLen(SalesHeader."No.")); // Parse document type from the JSON JSONManagement.GetStringPropertyValueByName('Document Type', ValueText); Evaluate(SalesDocumentType, ValueText); // Parse report ID from the JSON JSONManagement.GetStringPropertyValueByName('Report ID', ValueText); Evaluate(ReportID, ValueText); // Generate the report and return it in base64 format ReportBase64 := GetSalesReportBase64(SalesHeader, SalesDocumentType, DocumentNo, ReportID); end; // Function to generate the sales report in base64 format. // It takes as input the sales header record, document type, document number, and report ID. // It returns the report in base64 format. local procedure GetSalesReportBase64( var SalesHeader: Record "Sales Header"; var SalesDocumentType: enum "Sales Document Type"; var DocumentNo: Code[20]; var ReportID: Integer) Base64Text: Text; var Base64: Codeunit "Base64 Convert"; TempBlob: Codeunit "Temp Blob"; InStream: InStream; OutStream: OutStream; JsonObject: JsonObject; begin // Prepare the recordset based on the given document number and type SalesHeader.Reset(); SalesHeader.SetRange("No.", DocumentNo); SalesHeader.SetRange("Document Type", SalesDocumentType); // If no matching record is found, throw an error if not SalesHeader.FindLast() then Error('Sales Header %1 not found.', DocumentNo); // Generate the report and save it in a temporary blob TempBlob.CreateOutStream(OutStream); Report.SaveAs(ReportID, '', ReportFormat::Pdf, OutStream, GetRecRef(SalesHeader)); // Read the blob into a stream TempBlob.CreateInStream(InStream, TextEncoding::WINDOWS); // Convert the stream to base64 Base64Text := Base64.ToBase64(InStream); // Store the base64 string into a JSON object JsonObject.Add('Base64Pdf', Base64Text); JsonObject.WriteTo(Base64Text); // Return the base64 string exit(Base64Text); end; // This function returns a record reference from a variant. // The variant can be either a record or a record reference. local procedure GetRecRef(RecVariant: Variant) RecRef: RecordRef begin if RecVariant.IsRecordRef() then exit(RecVariant); if RecVariant.IsRecord() then RecRef.GetTable(RecVariant); end; }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 withinGetSalesOrderReportPDF
, 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
No comments:
Post a Comment