Tuesday, March 20, 2018

X++ code to run the report through code and send mail with attachments in D365


SRSPrintDestinationSettings     printerSettings;
Array                           arrayFiles;
System.Byte[]                   reportBytes = new System.Byte[0]();
SRSProxy                        srsProxy;
SRSReportRunService             srsReportRunService = new SrsReportRunService();
Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[]  parameterValueArray;
Map                             reportParametersMap;
SRSReportExecutionInfo          executionInfo = new SRSReportExecutionInfo();


filename = strfmt('%1%2',SSSSSS,'.pdf');
System.IO.Stream workbookStream = new System.IO.MemoryStream();
SysMailerSMTP           mailer = new SysMailerSMTP();
SysMailerMessageBuilder builder = new SysMailerMessageBuilder();
SysEmailParameters      parameters = SysEmailParameters::find();
SrsReportRunController  ssrsController = new SrsReportRunController();
XXXXXXXContract    Contract = new XXXXXXXContract();// define contract class

ssrsController.parmReportName(ssrsReportStr(ReportName, Design));
ssrsController.parmShowDialog(false);
Contract.parmRecordId(YYYY.RecId);//Record recid
ssrsController.parmReportContract().parmRdpContract(Contract);
printerSettings = ssrsController.parmReportContract().parmPrintSettings();
printerSettings.printMediumType(SRSPrintMediumType::File);
printerSettings.fileFormat(SRSReportFileFormat::PDF);
printerSettings.overwriteFile(true);
printerSettings.fileName(filename);
                 
ssrsController.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());
ssrsController.parmReportContract().parmReportExecutionInfo(executionInfo);
srsReportRunService.getReportDataContract(ssrsController.parmreportcontract().parmReportName());
srsReportRunService.preRunReport(ssrsController.parmreportcontract());
reportParametersMap = srsReportRunService.createParamMapFromContract(ssrsController.parmReportContract());
parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);

srsProxy = SRSProxy::constructWithConfiguration(ssrsController.parmReportContract().parmReportServerConfig());
// Actual rendering to byte array
reportBytes = srsproxy.renderReportToByteArray(ssrsController.parmreportcontract().parmreportpath(),
                                                                      parameterValueArray,
                                                                      printerSettings.fileFormat(),
                                                                      printerSettings.deviceinfo());

builder.setFrom(SysEmailParameters::find().SMTPUserName);
builder.addTo(AAAAAA);
builder.setSubject(strfmt(bbbbbbbb));
builder.setBody(content);
if (reportBytes)
{
     System.IO.Stream            stream         = new System.IO.MemoryStream(reportBytes);
     builder.addAttachment(stream,filename);
}
SysMailerFactory::getNonInteractiveMailer().sendNonInteractive(builder.getMessage());

No comments:

Post a Comment