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 :
Finance | Project Operations, Human Resources, ...
Answered

How to print multiple reports from code in succession

(0) ShareShare
ReportReport
Posted on by 235

On D365F&O  I have a requirement where the client wants multiple(3) reports to print automatically when the status is changed on a form.  I have been able to successfully print all the reports from code individually, and the download option pops up asking if I want to open or save the reports.  However when I try to run more than 1, only the last report pops up.

I have also tried using element.runAsync() with a class and moved the code to static methods and it returns the messages indicating the report printed successfully but no popup or download occurred.  Once again only the last report popped up and was I able to download it.

Can anyone give me advice on how to solve this, or if there is a setting to automatically download the report and not even have the interaction.

Below is example of my code for 1 of the reports and it works fine.  If I try to run this same report twice I would like to have it download the report twice

form...

//First run of report

element.runAsync(classNum(VRAutoPrint), staticMethodStr(VRAutoPrint,rep1),[VRParmTable],System.Threading.CancellationToken::None, formMethodStr(FormABC,FormCallBackTest));

//Second run of report

element.runAsync(classNum(VRAutoPrint), staticMethodStr(VRAutoPrint,rep1),[VRParmTable],System.Threading.CancellationToken::None, formMethodStr(FormABC,FormCallBackTest));

.....

//method in class ..

public static container autoRemittance(container _callerParm)
{
Args rmArgs = new Args();
SRSPrintDestinationSettings rmSettings;
VRRep1PrintController rmController = new VRRep1PrintController();
VRParmTable parmTable = conPeek(_callerParm, 1);

rmArgs.record(parmTable );
//rmArgs.caller(this);

rmController.parmArgs(rmArgs);
rmController.parmReportName(ssrsReportStr(VRReport1,Report));
//rmController.parmExecutionMode(SysOperationExecutionMode::Asynchronous);
rmController.parmShowDialog(false);
rmController.parmLoadFromSysLastValue(false);

// Change print settings as needed
rmSettings = rmController.parmReportContract().parmPrintSettings();
rmSettings.printMediumType(SRSPrintMediumType::File);
rmSettings.fileFormat(SRSReportFileFormat::PDF);
rmSettings.fileName('Report1_' + parmTable.StateId + '.pdf');

// Execute the report
rmController.startOperation();
return ['report1 printed for :' + parmTable.StatetId];;
}

I have the same question (0)
  • Sergei Minozhenko Profile Picture
    23,093 on at

    Hi Sophia.r,

    What if you try to print reports to archive and after all reports will be printed create a downloadable zip package with all 3 reports from the archive?

  • Sophia.r Profile Picture
    235 on at

    Hi Sergei.  Thank you for your response.  Is there no other way to do this? Also can you explain why it will only print the last report, and why it is not possible to print more than one report in this way. Kind regards

  • Suggested answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi Sophia,

    Looks like there is a way to do it without printing to archive.

    There is a delegate SRSPrintDestinationSettingsDelegates.toSendFile used in RetailTransactionServiceSrsReportEventHandler.

    (report processing is done in RetailTransactionServiceAdvanceInvoiceReport.runAdvanceReport.)

    1. You need to add parmReportBytes (or similar) to contract of your reports (check example in RetailTransactionServiceSrsReportDataContract)

    2. Create new subsription to SRSPrintDestinationSettingsDelegates.toSendFile (like in RetailTransactionServiceSrsReportEventHandler)

    3. After report processed store somewhere result from contract.parmReportBytes for each report

    4. Create zip package and add prevously stored files (one of example community.dynamics.com/.../how-to-pack-a-multiple-word-files-in-a-single-zip-file-in-d365

    5. Send zip package via Dynamics.AX.Application.File::SendFileToUser method

  • Sophia.r Profile Picture
    235 on at

    Hi Sergei Thank you once again, but the object is not to have to unzip the files but to have them download seperately.  Is there no way to download mutiple reports one after the other as seperate files.  The requirement originated because the client does not want to have to go an print each report 1 by 1 to have it down load.  They want them to automatically download when they change the status.  They are complaining by the number of clicks that they have to do for the thousands of transactions that they do daily.

    With the above code it works great if I only do the one report.  It is when I try to run 2  or more  (repeat the code) then it only downloads the last one.  Why is that happening.

  • Verified answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi Sophia,

    It works for many reports as well, here is a quick example of the standard asset report which printed 3 times and 3 files downloaded.

    static void main(Args _args)
    {
        for (int i = 1; i <= 3; i  )
        {
            AssetAcquisitionContract rdpContract = new AssetAcquisitionContract();
    
            AssetAcquisitionController controller = new AssetAcquisitionController();
            controller.parmReportName(ssrsReportStr(AssetAcquisition, Report));
            controller.parmReportContract().parmRdpContract(rdpContract);
    
            SRSPrintDestinationSettings printDestinationSettings = new SRSPrintDestinationSettings();
            printDestinationSettings.parmFileName('Report1_'   int2Str(i)   '.pdf');
            printDestinationSettings.fileFormat(SRSReportFileFormat::PDF);
            printDestinationSettings.printMediumType(SRSPrintMediumType::File);
    
            TestContract reportDataContract = new TestContract();
            reportDataContract.unpack(controller.parmReportContract().pack());
            reportDataContract.parmReportExecutionInfo(new SRSReportExecutionInfo());
            reportDataContract.parmPrintSettings(printDestinationSettings);
    
            new SrsReportRunService().runReport(reportDataContract);
    
            Browser br = new Browser();
    
            System.IO.Stream stream = new System.IO.MemoryStream(reportDataContract.parmReportBytes());
    
            str downloadurl = File::SendFileToTempStore(stream, printDestinationSettings.parmFileName(), classstr(FileUploadTemporaryStorageStrategy), true);
    
            new Browser().navigate(downloadurl, true, false);
    
        }
    }

    class TestContract extends SrsReportDataContract
    {
        public System.Byte[] reportBytes;
        
        [SubscribesTo(classStr(SRSPrintDestinationSettingsDelegates), delegateStr(SRSPrintDestinationSettingsDelegates, toSendFile))]
        public static void toSendFile(
            System.Byte[] reportBytes,
            SrsReportRunPrinter printer,
            SrsReportDataContract dataContract,
            Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[] paramArray,
            EventHandlerResult result)
        {
            if (dataContract is TestContract)
            {
                TestContract retailDataContract = dataContract as TestContract;
                retailDataContract.parmReportBytes(reportBytes);
            }
    
            result.result(false); // Stop further processing of report
        }
    
        public System.Byte[] parmReportBytes(System.Byte[] _reportBytes = reportBytes)
        {
            reportBytes = _reportBytes;
            return reportBytes;
        }
    
    }

  • Sophia.r Profile Picture
    235 on at

    Hi Sergei.  Thank you very much.  This last solution worked perfectly.  Thank you also for the code example.  I will now have a happy customer,

  • Armela Kamenica Profile Picture
    231 on at

    Sophia

    I am trying to accomplish the same thing using Business Central 365. Can you please share your code or how you got this to work.

    I am printing 2 reports.  Only the last one goes to the downloads folder. I would like both to be created and placed in the downloads folder.

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 503 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 434 Super User 2025 Season 2

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 278 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans