Skip to main content

Notifications

D365FO Send Report as email attachment

Sukrut Parab Profile Picture Sukrut Parab 71,656 Moderator

Hi All ,

Usually in any implementation project we get request to send Report as a attachment in email.  Below is code going to help you to send  report as an email attachment.  In the below code I am saving report in report archive(DocuRef table ) reading it from DocuRef table  in to bytes and converting it in to memory stream. The memory stream is further I am going to use to  send an email with Subject and body using SysMailerMessageBuilder class. You can use following methods of this class

setSubject - To set Subject of the email.

setBody -  To Set email body .

setFrom - To set sender of the email.

sendInteractive - To send interactively.

sendNonInteractive - TO send non interactively

 

        Args                                    args;
        SalesInvoiceContract                    salesInvoiceContract;
        CustInvoiceJour                         custInvoiceJourLocal;
        SRSPrintDestinationSettings             SRSPrintDestinationSettings;
        #SRSFramework

body = "Hello,<o:p></o:p></p><p></p><p>Please find the attached report. Please review and take necessary actions. <o:p></o:p></p><p></p><p>Regards,<o:p></o:p></p><p></p><p><span style='font-size:12.0pt'>AX Support Team<o:p></o:p></span></p>'; args = new Args(); select firstOnly custInvoiceJourLocal where custInvoiceJourLocal.InvoiceId == caaTrustFollowUpInvoices.InvoiceId; args.record(custInvoiceJourLocal); SrsReportRunController salesInvoiceController = new SrsReportRunController(); salesInvoiceController.parmArgs(args); salesInvoiceController.parmReportName(PrintMgmtDocType::construct(PrintMgmtDocumentType::SalesOrderInvoice).getDefaultReportFormat()); salesInvoiceController.parmShowDialog(false); salesInvoiceController.parmLoadFromSysLastValue(false); salesInvoiceContract = salesInvoiceController.parmReportContract().parmRdpContract(); salesInvoiceContract.parmRecordId(custInvoiceJourLocal.RecId); // Record id must be passed otherwise the report will be empty SrsReportDataContract srsReportDataContract = salesInvoiceController.parmReportContract(); //srsReportDataContract.parmisMemoryStreamOnly(true); salesInvoiceController.parmReportContract(srsReportDataContract); SRSPrintDestinationSettings = new SRSPrintDestinationSettings(); SRSPrintDestinationSettings.overridePrintContractSettings(true); SRSPrintDestinationSettings.printMediumType(SRSPrintMediumType::Archive); SRSPrintDestinationSettings.parmOverwriteFileIsSet(true); SRSPrintDestinationSettings.fileFormat(SRSReportFileFormat::PDF); SRSPrintArchiveContract SRSPrintArchiveContract = new SRSPrintArchiveContract(SRSReportFileFormat::PDF); SRSPrintDestinationSettings.parmSRSPrintArchiveContract(SRSPrintArchiveContract); str archiveName = strFmt('%1-%2', curUserId(), timeNow()); salesInvoiceController.parmReportContract().parmReportCaption(archiveName);//**This is the 'Description' field in the archive salesInvoiceController.parmReportContract().parmPrintSettings(SRSPrintDestinationSettings); salesInvoiceController.run(); PrintJobHeader printJobHeader; select firstonly forupdate printJobHeader where printJobHeader.jobDescription == archiveName; DocuRef docuRef; select firstonly forupdate docuRef where docuRef.RefRecId == PrintJobHeader.RecId && docuRef.RefTableId == tableNum(PrintJobHeader) && docuRef.TypeId == #SRSArchiveDocument; DocuValue docuValue; select firstonly forupdate docuValue where docuValue.RecId == docuRef.ValueRecId; container reportBytes = docuValue.File; System.Byte[] binData; System.IO.Stream stream; // Turn the Bytes into a stream for(int i = 0; i < conLen(reportBytes); i++) { binData = conPeek(reportBytes,i+1); stream = new System.IO.MemoryStream(binData); } ttsbegin; printJobHeader.delete(); docuRef.delete(); docuValue.delete(); ttscommit; infolog.clear();//Clear the message about archiving the report var messageBuilder = new SysMailerMessageBuilder(); messageBuilder.addTo(sendToEmail) .setSubject(Subject) .setBody(Body) .addCC(HcmWorker::find(ccEmail).email()); if (sendFromEmail) { messageBuilder.setFrom(sendFromEmail); } if (stream != null) { messageBuilder.addAttachment( stream, 'Invoice.pdf'); } SysMailerFactory::sendNonInteractive(messageBuilder.getMessage());

Comments

*This post is locked for comments

  • Tony Mok Profile Picture Tony Mok 577
    Posted at
    Hi BINAY KUMAR PANDEY, I think it is too late but I would like to share the how to attach pdf by DocuRef. First of all, select firstonly forupdate docuRef where docuRef.RefRecId == PrintJobHeader.RecId is used instead. As docuValue.File returns null, stream = DocumentManagement::getAttachmentStream(docuRef); is used instead.
  • BINAY KUMAR PANDEY Profile Picture BINAY KUMAR PANDEY 135
    Posted at
    I have tried the same above , code but I am getting docuValue.File = NULL , any suggestion ?.
  • BINAY KUMAR PANDEY Profile Picture BINAY KUMAR PANDEY 135
    Posted at
    I have the same requirement , I have asked in the forum today, can you please suggest where shouldi write the above code to meet my requirement . I want to send my Sales Invoice report as PDF immediately after posting the sales invoice by email along with I want to send my Product Safety sheet attachemnt in the same mail for my Item used in Sales Invoice , can you please brief the steps for that .