web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

How to print report with filters using Report.SaveAs()?

(0) ShareShare
ReportReport
Posted on by 82

Hello fellow developer,

I am trying to download a Sales Confirmation report automatically when Sales Header being released without using RunRequestPage().

Instead, I am using RecordRef. Below is my code but whenever the report is generated, it is still generating for all Sales Header records and not seem to be picking up my SalesHeader filter:

codeunit 50205 MyCodeunit
{
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Release Sales Document", 'OnAfterPerformManualCheckAndRelease', '', false, false)]
    local procedure AutoPrintReport(SalesHeader: Record "Sales Header")
    var
        ReportToPrint: Report "Sales Order";
        TempBlob: Codeunit "Temp Blob";
        LayoutSelection: Record "Report Layout Selection";
        ReportOutstream: OutStream;
        ReportParameter: text;
        FileManagement: Codeunit "File Management";
        SORecordReference: RecordRef;
        SalesHeaderFilter: Record "Sales Header";
    begin
        Clear(ReportOutstream);
        Clear(ReportToPrint);

        TempBlob.CreateOutStream(ReportOutstream);
        SORecordReference.GetTable(SalesHeader);
        LayoutSelection.SetTempLayoutSelected('1305-000001');

        ReportToPrint.SaveAs('', ReportFormat::Pdf, ReportOutstream, SORecordReference);
        FileManagement.BLOBExport(TempBlob, Format(ReportToPrint) + '_' + Format(CURRENTDATETIME, 0, '<Day,2><Month,2><Year4><Hours24><Minutes,2><Seconds,2>') + '.pdf', true);

    end;
}
 
Can someone please help me to point out if I used the RecordRef.GetTable() or Report.SaveAs() incorrectly? Or there is no way of printing report for specific record without using the Report Parameters.
I have the same question (0)
  • Verified answer
    YUN ZHU Profile Picture
    98,231 Super User 2026 Season 1 on at

    Hi, the following may not be the same as your question, but I think it should give you some hints.

    How to directly export Report from page (Custom Filename For Report Export)

    https://yzhums.com/21202/

    Thanks.

    ZHU

  • Verified answer
    Ginger Frog Profile Picture
    82 on at

    Hi Yun Zhu,

    Thanks a lot for the link. When I followed your code example the SaveAs magically worked.

    I had to use

    Report.SaveAs(Report::"Sales Order", '', ReportFormat::Pdf, ReportOutstream, SalesOrderRecordRef);

    instead of

    ReportVariable.SaveAs('', ReportFormat::Pdf, ReportOutstream, SalesOrderRecordRef);

    While all other code remains the same. I am not sure why the same SaveAs works for the first method but not the 2nd one but I am good for now.

    Thanks again!

    Ginger

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 1,960 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,084 Super User 2026 Season 1

#3
Dhiren Nagar Profile Picture

Dhiren Nagar 1,047 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans