Print multiple reports on business central

1.9k Views Asked by At

I need to use multiples Report.Print() in a code, but business central only prints the last requested print.

Using a dialog.confirm(), a page.runmodal() or a message() between the prints works, but I need the code to run automatically without user input.

Any ideas?

Ex.: Not working, only last one prints

codeunit 90101 Test
{
    trigger OnRun()
    var
        salesInvoice: Record "Sales Invoice Header";
        RecRef: RecordRef;
    begin
        salesInvoice.setfilter("No.", '103021');
        RecRef.GetTable(salesInvoice);
        Report.Print(1306, '', '', RecRef);
        salesInvoice.Reset();
        salesInvoice.setfilter("No.", '103022'); //only this one prints
        RecRef.GetTable(salesInvoice);
        Report.Print(1306, '', '', RecRef);
    end;

}

Ex2.: working, prints both

codeunit 90101 Test
{
    trigger OnRun()
    var
        salesInvoice: Record "Sales Invoice Header";
        RecRef: RecordRef;
    begin
        salesInvoice.setfilter("No.", '103021');
        RecRef.GetTable(salesInvoice);
        Report.Print(1306, '', '', RecRef);
        salesInvoice.Reset();
        salesInvoice.setfilter("No.", '103022');
        RecRef.GetTable(salesInvoice);
        if Dialog.Confirm('hello') then;
        Report.Print(1306, '', '', RecRef);
    end;

}
1

There are 1 best solutions below

0
On

I'm having the same issue. I was just as confused as I kept thinking "but how does report selection work then?" but realized it always opens the request page.

I'm considering these 2 options:

  1. Save as pdf and store it to a blob and print it using a .net service running on the application server. See this article for printing pdf's.
  1. Create a non-recurring job queue entry to print each report.

This is worked out quite well for me:

 ...
 CreatePrintingJob(Report::"Warehouse Shipment", WarehouseShipmentHeader.RecordId);
 ...


procedure CreatePrintingJob(_ReportID: Integer; _RecordToPrint: RecordId)
begin
    CreatePrintingJob(_ReportID, '', _RecordToPrint);
end;

procedure CreatePrintingJob(_ReportID: Integer; _ReportParameters: Text; _RecordToPrint: RecordId)
var
    jobQueueEntry: Record "Job Queue Entry";
begin
    jobQueueEntry.Init();
    jobQueueEntry."Object Type to Run" := jobQueueEntry."Object Type to Run"::Report;
    jobQueueEntry."Object ID to Run" := _ReportID;
    jobQueueEntry."Record ID to Process" := _RecordToPrint;
    jobQueueEntry."Starting Time" := 000000T;
    jobQueueEntry."Maximum No. of Attempts to Run" := 3;
    jobQueueEntry."Report Output Type" := jobQueueEntry."Report Output Type"::Print;
    jobQueueEntry.Insert(true);
    jobQueueEntry.SetReportParameters(_ReportParameters);
    jobQueueEntry.ScheduleTask();
end;