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();

16 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

        • Hello,
          I need also to save the pdf file (report) from D365 to local directory.I did not have issue but the file is downloaded and i should save it manually. the path given by code is ignored.
          There is a solution,please ?
          Thank you in advance.

          • The web application can’t simply connect to your local machine and change files there. Therefore your solution simply isn’t possible.

  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?

    • Hi Mohammad, I suggest you should create a thread in a discussion forum (such as at https://community.dynamics.com/) and explain your requirement in detail there. I participate in these forums and you’ll also reach other people there. And don’t forget to mention your version of AX.

  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).

    • It still works correctly for me in 10.0.35.
      Isn’t your browser downloading the file automatically without you noticing?

      • 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.

        • The above probblem has been resolved.
          But I have another problem. When downloading some file on one click, some file doesn’t downloaded On Hyper-V, Cloud-hosted environment. It’s instable, and at times only the first file and the last file are downloaded. At other times, diffrent files are downloaded. On Virtual BOX, this doesn’t recur.
          Do you have any thoughts about what the cause might be?

  5. Hi Martin,
    My requirement is to get multiple reports printed on a single button clicked and to be printed at a single time in ax-2012. Can you help me out in this with the code?

    Thanks in advance!

Comments are closed.