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

Notifications

Announcements

No record found.

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
    95,729 Super User 2025 Season 2 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,362

#2
YUN ZHU Profile Picture

YUN ZHU 867 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 607

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans