Generate DoCentric AX report and send to customer throw Email using email template

In this requirement, we need to generate a Docentric report for salesInvoice report and send the report as a email attachment using the SysEmailTemplates. Here is the X++ code to generate a Docentric report stream and send it as email attachment.  

internal final class DaxSendDocCentricInvoiceReport
{
    #PrintMgmtSetup
    public static void printSalesInvoiceToMemory(CustInvoiceJour  _custInvoiceJour)
    {
        SalesInvoiceController     salesInvoiceController;
        SalesInvoiceContract       salesInvoiceContract;
        Args                       args = new Args();
        CustInvoiceJour            custInvoiceJour;
        SalesInvoiceJournalPrint   salesInvoiceJournalPrint;
 
       // select custInvoiceJour where custInvoiceJour.SalesId != '';
 
        // Add record to be printed.
        // In order to have the context table we need to set args.record().
        args.record(_custInvoiceJour);
 
        salesInvoiceController = SalesInvoiceController::construct();
        salesInvoiceController.parmReportName(
        PrintMgmtDocType::construct(PrintMgmtDocumentType::SalesOrderInvoice).getDefaultReportFormat());
 
        salesInvoiceContract = salesInvoiceController.parmReportContract().parmRdpContract();
        salesInvoiceContract.parmCountryRegionISOCode(SysCountryRegionCode::countryInfo());
        salesInvoiceController.parmArgs(args);
        salesInvoiceController.parmExecutionMode(SysOperationExecutionMode::Synchronous);
 
        SRSPrintDestinationSettings pds = salesInvoiceController.parmReportContract().parmPrintSettings();
        pds.printMediumType(SRSPrintMediumType::Memory_DC);

        //PrintMgmtPrintContext printContext = new PrintMgmtPrintContext();

        //PrintMgmtPrintDestinationTokens destinationTokens = SrsPrintDestinationTokens::constructByTokenType(#SRSPrintDestinationTokenPrintMgmt);
        //destinationTokens.parmPrintContext(printContext);
        //destinationTokens.replace(pds);

 
        // Use the default Docentric design.
        pds.parmMemoryPrintDestSettings_DC().parmOutputFileFormat(DocOutputFileFormat::PDF);
        pds.parmMemoryPrintDestSettings_DC().parmUseSsrsBuiltInDesign(false);
        pds.parmSrsPrintReportSettings_DC().setProperty_PrintSrsOriginalDesign(false);


        /* Uncomment this code if you want to use the original SSRS design instead */
        //pds.parmMemoryPrintDestSettings_DC().parmOutputFileFormatSrs(SRSReportFileFormat::PDF);
        //pds.parmMemoryPrintDestSettings_DC().parmUseSsrsBuiltInDesign(true);
        //pds.parmSrsPrintReportSettings_DC().setProperty_PrintSrsOriginalDesign(true);
    
        // Initalize SalesInvoiceJournalPrint class instance because there is no other way
        // NOT to use Print Management.
        salesInvoiceJournalPrint = SalesInvoiceJournalPrint::construct();
        salesInvoiceJournalPrint.parmPrintFormletter(NoYes::Yes);
        salesInvoiceJournalPrint.parmUsePrintManagement(false);
        salesInvoiceJournalPrint.parmUseUserDefinedDestinations(true);
        salesInvoiceJournalPrint.parmPrinterSettingsFormLetter(salesInvoiceController.parmReportContract().parmPrintSettings().pack());
 
        args.caller(salesInvoiceJournalPrint);
        args.parmEnumType(enumNum(PrintCopyOriginal));
        args.parmEnum(PrintCopyOriginal::OriginalPrint);
 
        /* Collect the result of the execution */
        // Register event handler to the RenderingComplete event.
        salesInvoiceController.renderingCompleted +=
        eventhandler(DaxSendDocCentricInvoiceReport::printSalesInvoiceToMemory_renderingComplete);
 
        // Start the report execution.
        salesInvoiceController.parmShowDialog(false);
        salesInvoiceController.startOperation();
    }

    public static void printSalesInvoiceToMemory_renderingComplete(SrsReportRunController _sender, SrsRenderingCompletedEventArgs _eventArgs)
    {
        // Get the report execution info.
        DocReportExecutionInfo reportExecutionInfo = _eventArgs.parmReportExecutionInfo().parmReportExecutionInfo_DC();
        DocPrintReportToMemoryExecutionInfo printDestMemoryExecutionInfo = reportExecutionInfo.parmPrintToMemoryExecutionInfo();

        // Use the report content, e.g. upload it to Azure Blob storage.
        using (System.IO.MemoryStream reportMemoryStream = printDestMemoryExecutionInfo.getReportContentAsMemoryStream())
        {
           // File::SendFileToUser(reportMemoryStream, "TestInvoice.PDF");
            PROSendDocCentricInvoiceReport::sendInvoiceByEmail(_eventArgs.parmArgs().record(), "thirupathi.gandi@protivitiGlobal.in", reportMemoryStream );
           // DocAzureBlobHelper::uploadBlob('docentric-report-output', 'TestInvoice.pdf', reportMemoryStream);
        }

        // Do something else with the report content, e.g. send it to a web service...
    }

    public static void sendInvoiceByEmail(CustInvoiceJour _custInvoiceJour, Email _ToEmail, System.IO.MemoryStream _reportStream )
    {
        SysEmailParameters              sysEmailParameters = SysEmailParameters::find();
        #define.SMTP('SMTP')
        Map sysMailers = SysMailerFactory::getMailers();
        SysEmailSystemTable SysEmailSystemTable = SysEmailSystemTable::find("SalesInv");

        SysEmailMessageSystemTable SysEmailMessageSystemTable = SysEmailMessageSystemTable::find("SalesInv",SysEmailSystemTable.DefaultLanguage);

        str messageBody = SysEmailMessageSystemTable.Mail;
        str subject     = SysEmailMessageSystemTable.Subject + '' + _custInvoiceJour.InvoiceID;

        if (SysEmailMessageSystemTable)
        {
            SysMailerMessageBuilder messageBuilder = new SysMailerMessageBuilder();
            try
            {

                messageBuilder.setBody(messageBody);
                messageBuilder.setSubject(subject);
                messageBuilder.addTo(_ToEmail);
                messageBuilder.addCc("thiruvaroon555@gmail.com");
                messageBuilder.addAttachment(_reportStream, strFmt("Sales Invoice %1.PDF", _custInvoiceJour.InvoiceId));
                messageBuilder.setFrom(sysEmailParameters.SMTPUserName);

                SysMailerFactory::sendNonInteractive(messageBuilder.getMessage());
            }
            catch
            {
                exceptionTextFallThrough();
            }
        }
        else
        {
            throw error("Please verify the email template for SalesInvoice");
        }

    }
Thanks!!

No comments:

Post a Comment