Monday 13 October 2014

SSRS Report Auto e-mail and Save as file tasks in reports in ax 2012

Saving the report to a file
To save the file to the printer, carry out the following steps:
1. Create a new static method in the controller class or in the class from where the
report is invoked.
2. On the method, place the following code to save the report. Here the file location is
hard coded but you may want to turn it into a parameter or refer to a table location:
public static void saveReporttoFile(Args _args)
{
/*if the controller is not overriden for your report then use
appropriate controller*/
SrsReportRunController controller = new
PktRdlCustTransListController();
SRSPrintDestinationSettings printSettings;
controller.parmReportName(ssrsReportStr(PktRdlCustTransList,
CustTransList));
// get print settings from contract
printSettings = controller.parmReportContract().
parmPrintSettings();
// set print medium
printSettings.printMediumType(SRSPrintMediumType::File);
printSettings.fileFormat(SRSReportFileFormat::PDF);
printSettings.overwriteFile(true);
printSettings.fileName(@"C:\Temp\CusttransReport.pdf");
// suppress the parameter dialog
controller.parmShowDialog(false);
controller.startOperation();
}


Sending the report through an e-mail
To send the file through an e-mail the procedure is not much different except the need to pass
an e-mail contract:
public static void mailReport(Args _args)
{
SrsReportRunController controller = new
PktRdlCustTransListController();
SRSPrintDestinationSettings printSettings;
SrsReportEMailDataContract emailContract;
// set report name
controller.parmReportName(ssrsReportStr(PktRdlCustTransList,
CustTransList));
// create email contract
emailContract = new SrsReportEMailDataContract();
// fill in the email contract details
emailContract.parmAttachmentFileFormat(SRSReportFileFormat::PDF);
emailContract.parmSubject("Customer Transactions");
emailContract.parmTo("admin@contoso.com");
printSettings = controller.parmReportContract().
parmPrintSettings();
printSettings.printMediumType(SRSPrintMediumType::Email);
printSettings.parmEMailContract(emailContract);
printSettings.fileFormat(SRSReportFileFormat::PDF);
// suppress the parameter dialog
controller.parmShowDialog(false);
controller.startOperation();
}

server static void PrintSSRSreport(Args _args)
{
    SrsReportRunController  Controller;
    Pwc_ARCustomerStatementContract    StatementContract;
    Args                    args = new Args();
    SrsReportRunImpl        srsReportRun;
    CustInvoiceJour         custInvoiceJour;
    ReportName              reportName = "ARInboundMaster.DesignAR";

    TransDate     startvalue,datevalue,startvaluelast;
    boolean showDialog;
    ;
    startvalue = dateStartMth(today())-1;
    startvaluelast =dateStartMth(startvalue);
    datevalue = startvalue;


    Controller = new SrsReportRunController();


    Controller.parmReportName(reportName);
    StatementContract = Controller.parmReportContract().parmRdpContract();
    StatementContract.parmFromdate(startvaluelast); // Record id must be passed otherwise the report will be empty
    StatementContract.parmTodate(datevalue); // comment this code if tested in pre release
    Controller.parmArgs(args);
    srsReportRun = Controller.parmReportRun() as SrsReportRunImpl;
    Controller.parmReportRun(srsReportRun);
    Controller.parmReportContract().parmPrintSettings().printMediumType(SRSPrintMediumType::File);
    Controller.parmReportContract().parmPrintSettings().overwriteFile(true);
    Controller.parmReportContract().parmPrintSettings().fileFormat(SRSReportFileFormat::PDF);
    Controller.parmReportContract().parmPrintSettings().fileName(@"C:\CusttransReport.pdf");
    Controller.runReport();

}


No comments:

Post a Comment