Business Central

Microsoft Dynamics 365 Business Central – Save Report As PDF, Excel, XML, Word or HTML

Old report methods like SaveAsPdf, SaveAsExcel. SaveAsXML, SaveAsWord or SaveAsHTML are now not supported in Business Central (SaaS). They were supported on Business Central On-Premise or older Dynamics NAV versions. If we use these methods in Business Central SaaS then we will get error message “The type or method ‘SaveAsPdf’ cannot be used for ‘Extension’ development.AL (AL0296).

For the security reason, MS is not allowing to create temporary file on SaaS version of D365BC hence on we cannot use any of these methods in Business Central (SaaS). here is a workaround to run a specific report without a request page and save it as a PDF, Excel, Word, HTML, or XML file.

Use method Report.SAVEAS, it stores output of desired reportin the OutStream data type and from OutStream we can simply export / download to local machine. Or if require we can attach the Stream value to Email as well. Below is the sample code on for downloading / exporting report as PDF to local machine.

Action("Export Blocked Customers")
{
    trigger OnAction()
    var
        TempBlob_lRec: Codeunit "Temp Blob";
        BlobOutStream: OutStream;
        RecRef: RecordRef;
        FileMgt: Codeunit "File Management";
        CustRec: Record Customer;
    begin
        TempBlob_lRec.CreateOutStream(BlobOutStream, TEXTENCODING::UTF8);
        CustRec.Reset;
        CustRec.SetRange(Blocked, Blocked::All);
        RecRef.GetTable(CustRec);
        REPORT.SAVEAS(101, '', REPORTFORMAT::Pdf, BlobOutStream, RecRef);
        FileMgt.BLOBExport(TempBlob_lRec, 'Blocked Customers.pdf', TRUE);
    end;
}

Rachlin Thomas

Sr. Software Developer

Share
Published by
Rachlin Thomas