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.
  • Verified answer
    Ginger Frog Profile Picture
    82 on at
    RE: How to print report with filters using Report.SaveAs()?

    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

  • Verified answer
    YUN ZHU Profile Picture
    79,097 Super User 2025 Season 1 on at
    RE: How to print report with filters using Report.SaveAs()?

    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

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,207 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,923 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans