Dynamics 365 Business Central: How to directly export Report from page (Custom Filename For Report Export)

Dynamics 365 Business Central

Hi, Readers.
I was asked a interesting question yesterday, is there any way to customize the filename when exporting the report?

As you know, with standard feature in Business Central, we can choose Send to… in the Request page of the report. (A request page is a page that is run before the report starts to execute)

Choose the file type. (The file types depending on the Layout type)

Then the exported file will be downloaded.

The file name looks like the following…:
Sales – Confirmation (1).pdf
Sales – Confirmation (2).pdf
Sales – Confirmation (3).pdf

PS: Files in Web Client are usually downloaded automatically, but you can change the browser setting so that you can enter the name manually before downloading.
For example: Chrome

And I noticed a similar idea in Dynamics 365 Application Ideas from two years ago. (You can vote in the following link)
Custom Filename For Report Export

So if the client really needs this feature now, is there another way to do it?
Yes, in my perious post How to save the report as a PDF, Excel, Word, HTML, or XML file, we discussed how to use Report.SaveAs() Method to export the report via AL. This time we can do some simple improvements. Let’s try to directly export Report from page with custom filename.

Test Requirement: Export the selected sales orders in the Sales Order list in PDF format with the name format Sales Order + UserID + Datetime.

Test Video:

Source Code: You can set the report format as you need (PDF, Excel, Word, HTML, or XML file).

pageextension 50100 ZYSalesOrderExt extends "Sales Order List"
{
    actions
    {
        addafter("Print Confirmation")
        {
            action(SaveConfirmationAsPdf)
            {
                Caption = 'Save Confirmation As Pdf';
                ApplicationArea = All;
                Image = Export;
                Promoted = true;
                PromotedCategory = Category8;
                PromotedIsBig = true;

                trigger OnAction()
                var
                    TempBlob: Codeunit "Temp Blob";
                    FileManagement: Codeunit "File Management";
                    OStream: OutStream;
                    SalesHeader: Record "Sales Header";
                    RecRef: RecordRef;
                begin
                    SalesHeader.Reset();
                    Clear(OStream);
                    CurrPage.SetSelectionFilter(SalesHeader);
                    RecRef.GetTable(SalesHeader);
                    TempBlob.CreateOutStream(OStream);
                    Report.SaveAs(Report::"Standard Sales - Order Conf.", '', ReportFormat::Pdf, OStream, RecRef);
                    FileManagement.BLOBExport(TempBlob, 'Sales Order_' + UserId + '_' + Format(CurrentDateTime, 0, '<Year4><Day,2><Month,2><Hours24><Minutes,2><Seconds,2>') + '.pdf', true);
                end;
            }
        }
    }
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

タイトルとURLをコピーしました