Send Email with PO confirmation report with attachment
class PurchPurchaseEmailController extends SrsReportRunController
{
public static PurchPurchaseOrderController construct()
{
return new PurchPurchaseOrderController();
}
public static void main(Args _args)
{
SrsReportRunController formLetterController = PurchPurchaseOrderController::construct();
PurchPurchaseOrderController controller;
DocuRef docuref, addedRecord;
SRSPrintDestinationSettings settings;
SysMailerMessageBuilder mailer = new SysMailerMessageBuilder();
SysMailerExchange smtp = new SysMailerExchange();
str Body, toMail1;
Filename fileName;
Array arrayFiles;
SysInfologEnumerator sysInfologEnumerator;
Log errorMessage;
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();
_args.record(VendPurchOrderJour::findRecId(_args.record().RecId));
VendPurchOrderJour vend = VendPurchOrderJour::findRecId(_args.record().RecId);
vendTable vendtable;
select vendtable where vend.OrderAccount == vendtable.AccountNum;
info(strFmt('%1', vendTable.email()));
controller = formLetterController;
controller.parmArgs(_args);
controller.parmReportName(PrintMgmtDocType::construct(PrintMgmtDocumentType::PurchaseOrderConfirmationRequest).getDefaultReportFormat());
controller.parmShowDialog(false);
formLetterController.startOperation();
fileName = 'Receipt.PDF';
settings = controller.parmReportContract().parmPrintSettings();
settings.printMediumType(SRSPrintMediumType::File);
settings.fileName(fileName);
settings.fileFormat(SRSReportFileFormat::PDF);
controller.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());
controller.parmReportContract().parmReportExecutionInfo(executionInfo);
srsReportRunService.getReportDataContract(controller.parmreportcontract().parmReportName());
srsReportRunService.preRunReport(controller.parmreportcontract());
reportParametersMap = srsReportRunService.createParamMapFromContract(controller.parmReportContract());
parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);
srsProxy = SRSProxy::constructWithConfiguration(controller.parmReportContract().parmReportServerConfig());
reportBytes = srsproxy.renderReportToByteArray(controller.parmreportcontract().parmreportpath(),
parameterValueArray,
settings.fileFormat(),
settings.deviceinfo());
if (reportBytes)
{
// Create a memory stream from the report bytes
System.IO.MemoryStream stream = new System.IO.MemoryStream(reportBytes);
// Send the file to the user
File::SendFileToUser(stream, settings.parmFileName());
// Attach the file to the record
str fileContentType = System.Web.MimeMapping::GetMimeMapping(fileName);
addedRecord = DocumentManagement::attachFile(vend.TableId, vend.RecId, vend.DataAreaId, "File", stream, fileName, fileContentType, vend.OrderAccount + "Receipt.PDF");
}
// Prepare email
toMail1 = vendtable.email();
mailer.setSubject('Purch');
mailer.setFrom(SysEmailParameters::find().SMTPUserName);
mailer.setBody("Nothing");
mailer.addTo(toMail1);
mailer.addAttachment(DocumentManagement::getAttachmentStream(addedRecord), addedRecord.Name);
// Send email
smtp.sendNonInteractive(mailer.getMessage());
// Attach additional document
select docuref where docuref.RefRecId == vend.RecId
&& docuref.RefTableId == vend.TableId
&& docuref.RefCompanyId == curExt();
mailer.addAttachment(DocumentManagement::getAttachmentStream(docuRef),
strFmt('%1%2', docuRef.Name, '.'+docuref.fileExtension()));
}
}
Error Show - This stream does not support seek operations.
Please suggest