Thursday, 19 December 2013

Save SSRS report to pdf that uses Controller classes [Dynamics AX 2012]

­­­­­
This is the most commonly asked requirement of saving the report to pdf and send it to through email.
We all know that SRSReportRun class is helpful in combination with printDestinationsettings to save to pdf file.
But tricky requirements are those which uses controller classes for the report. Below is the small snippet which will help to save the sales invoice report to pdf that actually uses controller classes.

static void SR_SaveReportToPDFFromController(Args _args)
{
    SalesInvoiceController  salesInvoiceController;
    SalesInvoiceContract    salesInvoiceContract;
    Args                    args = new Args();
    SrsReportRunImpl        srsReportRun;
    CustInvoiceJour         custInvoiceJour;
    ReportName              reportName = "SalesInvoice.Report";
    ;

    select firstOnly custInvoiceJour;
    args.record(custInvoiceJour);
   
    salesInvoiceController = new SalesInvoiceController();
    salesInvoiceController.parmReportName(reportName);
   
    salesInvoiceContract = salesInvoiceController.parmReportContract().parmRdpContract();
    salesInvoiceContract.parmRecordId(custInvoiceJour.RecId); // Record id must be passed otherwise the report will be empty
    salesInvoiceContract.parmCountryRegionISOCode(SysCountryRegionCode::countryInfo()); // comment this code if tested in pre release
    salesInvoiceController.parmArgs(args);

    srsReportRun = salesInvoiceController.parmReportRun() as SrsReportRunImpl;
   
    salesInvoiceController.parmReportRun(srsReportRun);
    salesInvoiceController.parmReportContract().parmPrintSettings().printMediumType(SRSPrintMediumType::File);
    salesInvoiceController.parmReportContract().parmPrintSettings().overwriteFile(true);
    salesInvoiceController.parmReportContract().parmPrintSettings().fileFormat(SRSReportFileFormat::PDF);
    salesInvoiceController.parmReportContract().parmPrintSettings().fileName(‘c:\\SR_SalesInvoice.pdf’);
    salesInvoiceController.runReport();
}
Below is the pdf file output:

image



1 comment: