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...
Suggested Answer

How to use AL to Download Multiple Reports from Sales Order List

(0) ShareShare
ReportReport
Posted on by 82

Hi fellow developers!

I am working on a project to auto download Sales Order report upon Sales Order Releases.

While the code works perfectly when ONE order is released, when we tried to mass release multiple Sales Orders by using the 'Select More' and 'Release' on the Sales Order List, only the last selected Sales Order report is downloaded.

I've tried running my codeunit in debug mode and confirmed the codeunit actually ran multiple times as expected. Every time the codeunit is ran with the right Sales Order Record till the last line but there was nothing downloaded until the last selected Sales Order Record is ran and a report is downloaded for last sales order only.

Below is my code:

codeunit 50205 "Download Report SO Release"
{
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Release Sales Document", 'OnAfterPerformManualCheckAndRelease', '', false, false)]
    local procedure DownloadReport(SalesHeader: Record "Sales Header")
    var
        SOReport: 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
        if SalesHeader."Sell-to Customer No." = 'Customer that requires report printing' then begin
            Clear(ReportParameter);
            Clear(ReportOutstream);
            Clear(SOReport);

            TempBlob.CreateOutStream(ReportOutstream);
            SalesHeaderFilter.SetFilter("No.", '=%1', SalesHeader."No.");
            SalesHeaderFilter.FindFirst();
            SORecordReference.GetTable(SalesHeaderFilter);
            LayoutSelection.SetTempLayoutSelected('10075-000001');
            Report.SaveAs(Report::"Sales Order", '', ReportFormat::Pdf, ReportOutstream, SORecordReference);
            FileManagement.BLOBExport(TempBlob, Format('SOReport') + '_' + Format(SalesHeader."External Document No.") + '_' + Format(CURRENTDATETIME, 0, '<Year4><Month,2><Day,2><Hours24><Minutes,2><Seconds,2>') + '.pdf', true);
        end;
    end;
}
Has anyone tried downloading multiple files using AL before that can help with the above? Thanks a lot in advance!
Thank you,
Ginger
I have the same question (0)
  • Amit Baru Profile Picture
    3,037 on at

    Hi,

    Pls refer the below link for AL Symbol download

    https://erpconsultors.com/issue-in-installing-al-symbol-in-vs-editor/

    Regards

    Amit Sharma

    www.erpconsultors.com

  • Suggested answer
    Inge M. Bruvik Profile Picture
    1,111 Moderator on at

    What version are you in and are you BC online or on prem.

    Instead of using filemanagement. Try using file.downloadfromstream

    learn.microsoft.com/.../file-downloadfromstream-method

  • Suggested answer
    Nitin Verma Profile Picture
    21,708 Moderator on at

    Hi,

    In your case if you want to download all the order's reports together, it will not allow you with the functions and method you use, it will run through the loop but would download only the last record's report, due to BC limititations.

    If you want to download all the reports all together in that case you have to use ZIP functionality so that system zip all the reports and then download the zip file.

     

    datacompresion: Codeunit "Data Compression";
    ZipFileName: text;
    ItemCnt: Integer;
    FileName: text;
    instrm: InStream;
    blobStorage: Codeunit "Temp Blob";
    ZipOutStream: OutStream;
    ZipInStream: InStream;

    repeat
    APIResponses.reset;
    APIResponses.SetRange("Manifest No.", YourREcord."No.");
    APIResponses.SetRange("Manifest Line No.", YourREcord."Line No.");
    APIResponses.SetRange("API Type", 'Label');
    if APIResponses.FindSet() then
    FileName := '';
    FileName := YourREcord."No." + '_' + format(YourREcord."Sequence No.") + '.pdf';
    Blobfield.CreateInStream(instrm);
    datacompresion.AddEntry(instrm, FileName);
    ItemCnt += 1;
    end;
    end;
    until YourREcord.Next() = 0;
    blobStorage.CreateOutStream(ZipOutStream);
    datacompresion.SaveZipArchive(ZipOutStream);
    datacompresion.CloseZipArchive();
    blobStorage.CreateInStream(ZipInStream);
    DownloadFromStream(ZipInStream, 'Download zip file', '', '', ZipFileName);
  • Ginger Frog Profile Picture
    82 on at

    Hi Inge,

    Thanks for the advice. I am on v20.4 Cloud version.

    I tired to use file.DownloadFromStream, it's behaving very similar to FileManagement.BlobExport. It is working perfectly for one file download but when multiple Sales Orders are selected, it only download one single report on the last Sales Order report generation.

    I am guessing it's a BC limitation.

    Thank you,

    Ginger

  • Suggested answer
    Amit Baru Profile Picture
    3,037 on at

    Hi,

    Yes you are right. I have discussed the above requirement with others.

    Regards

    Amit Sharma

  • Ginger Frog Profile Picture
    82 on at

    Hi Nitin!

    Thanks a lot it worked like a charm.

    Ginger.

  • Suggested answer
    Inge M. Bruvik Profile Picture
    1,111 Moderator on at

    Good to see you have it fixed with the download from streams with the Nitin Verma and then we established that it is not a limitation in BC!

    Keep up the good work.

  • Suggested answer
    YUN ZHU Profile Picture
    95,729 Super User 2025 Season 2 on at

    Hi, hope the following helps.

    https://yzhums.com/21735/

    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

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