Printing reports from code in D365FO

A comment below my blog post Printing reports from code in AX2012 asked me to provide an example for D365FO. Here is it.

The code is virtually identical. Only writing a file to a shared folder doesn’t make a good sense in cloud, therefore I changed the code to return the file to user for download.

SrsReportRunController          controller = new SrsReportRunController();
SysUserLicenseCountRDPContract  rdpContract = new SysUserLicenseCountRDPContract();
SRSPrintDestinationSettings     settings;
 
// Define report and report design to use
controller.parmReportName(ssrsReportStr(SysUserLicenseCountReport, Report));
// Use execution mode appropriate to your situation
controller.parmExecutionMode(SysOperationExecutionMode::Synchronous);
// Suppress report dialog
controller.parmShowDialog(false);
 
// Explicitly provide all required parameters
rdpContract.parmReportStateDate(systemDateGet());
controller.parmReportContract().parmRdpContract(rdpContract);
 
// Change print settings as needed
settings = controller.parmReportContract().parmPrintSettings();
settings.printMediumType(SRSPrintMediumType::File);
settings.fileFormat(SRSReportFileFormat::Excel);
settings.fileName('UserLicenseCount.xlsx');
 
// Execute the report
controller.startOperation();

12 Comments

  1. Hi Martin,
    public static void main(Args _args)
    {
    SrsReportRunController controller = new SrsReportRunController();
    CustTransOpenPerDateContract contract = new CustTransOpenPerDateContract();
    SrsReportEMailDataContract emailContract = new SrsReportEMailDataContract();
    ExchangeRateHelper exchangeRateHelper = new ExchangeRateHelper();
    SRSPrintDestinationSettings settings;

    controller.parmReportName(ssrsReportStr(FreeTextInvoice, Report));

    controller.parmExecutionMode(SysOperationExecutionMode::ScheduledBatch);

    controller.parmShowDialog(false);
    exchangeRateHelper.parmFromCurrency(“USD”);

    controller.parmReportContract().parmRdpContract(contract);

    settings = controller.parmReportContract().parmPrintSettings();
    settings.printMediumType(SRSPrintMediumType::File);
    settings.fileFormat(SRSReportFileFormat::PDF);
    settings.overwriteFile(true);
    settings.fileName(@’C:\Users\sst_siddhant\Desktop\PDFfile.pdf’);

    controller.startOperation();
    }
    I am getting an error –
    1.A currency to convert from is required to retrieve exchange rate information.
    2.Microsoft.Dynamics.Ax.Xpp.ErrorException: Exception of type ‘Microsoft.Dynamics.Ax.Xpp.ErrorException’ was thrown.
    What will be the solution for this issue.

    Thank You
    Siddhant Singh

  2. if you are using the FreeTextInvoice Report, you must populate the journal RecId from which to print the report. You are not putting anything in that contract. So how must D365 know which record to use for the report?

    • FreeTextInvoice reports uses FreeTextInvoiceContract, which has a method called parmCustInvoiceJourRecId(). The value is normally set in FreeTextInvoiceController.preRunModifyContract().

  3. It may be little late for comment but i am trying use this code. My scenario is slightly different.
    I don’t have report to print but I have HTML stream to print. I want to send that stream on print using Documentcontract but DocumentContractType does’t support HTML. Is there a way I can use this code to print that HTML on printer?

  4. I tried to use this code. Preview doesn’t show but download doesn’t start. Some Browser Settings are needed? D365FO App Version is 10.0.33(10.0.1549.38).

      • Thanks for replying.
        No file has been downloaded.
        I tried to download a PDF file of On-hand inventory report, changing destination on the dialog(no customize), but not downloaded. I think i should resolve this, and your code will go well. I’m going to ask mircosoft.

Leave a Reply

Your email address will not be published. Required fields are marked *