Printing reports from code in AX2012

How to print a report from X++ and change print settings in AX2012?

I have a concrete example for you – it should give a good idea how to do that even if your situation is a little bit different.

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::ScheduledBatch);
// 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(@'\\share\UserLicenseCount.xlsx');
 
// Execute the report
controller.startOperation();

10 Comments

  1. Hi Martin,

    I already done some similar kind of example. But in my case i am testing this through Job. Where i can set the report parameters (can take the parameter reference from VS ex:DatesetName_Parametername)in job. But how to use the dynamics parameters in X++ code. example code: This is working fine but i am unable to override default parameters added in query range(say itemid), its taking what is there in report run dailog

    static void GenerateSSRSansSave(Args _args)
    {
    SrsReportRun srsReportRun;
    //SysMailer mailer = new SysMailer();

    srsReportRun = new SrsReportRun(“ReportName.Report1”);

    srsReportRun.init();
    srsReportRun.reportCaption(“InventOnHand”);
    srsReportRun.reportParameter(“InventOnHandBGDS_ViewConfigId”).value(true);
    .
    .
    .

    srsReportRun.showDialog(false);

    // Print to a file named ReportExample in HTML/PDF format.
    srsReportRun.printDestinationSettings().printMediumType(SRSPrintMediumType::File);
    srsReportRun.printDestinationSettings().fileFormat(SRSReportFileFormat::PDF);
    srsReportRun.printDestinationSettings().overwriteFile(true);
    srsReportRun.printDestinationSettings().fileName(@”C:\InventOnHandReport.pdf”);

    if( srsReportRun )
    {
    srsReportRun.executeReport();
    }
    info(“Report Saved”);
    }

    Thanks In Advance.

  2. Martin,

    Thanks so much for this post it’s saved me! Been struggling with some custom printing stuff for a couple days now with 2012.

    I’m going to probably do a more detailed blog post based off of this.

    Alex

  3. Whenever I run this I always get an info box that says:

    “The report has been successfully saved as: ”

    how do I suppress this message?

  4. Thanks. But how to run this code? My requirement is I would like to print AX 2012 reports to Excel file in batch mode. I will need to supply report parameters (like Source system, Sorty by, Start date, End date etc). How can I pass the parameters in this code? Appreciate any help. Thanks

  5. Hi,

    Thanks for the code.
    Have you ever encountered trouble using parmShowDialog(false) to suppress the prompt?
    I’ve tried on a (custom) report and the dialog always showed up. I had to use parmLoadFromSysLastValue(false) otherwise the showDialog boolean was overwritten…
    See ya!
    Nicolas

    • Martin Dráb

      The Real Person!

      Author Martin Dráb acts as a real person and passed all tests against spambots. Anti-Spam by CleanTalk.

      Yes, I’ve seen it before. It’s apparently saved in the report state.

  6. Hello Martin,

    Could you give me a example for D365. Should I create a class for that code?

  7. Hi Martin,

    Do you know if it’s possible to print the report on both sides of a page w/ code?

Comments are closed.