Loading...

D365 FO - Send Email with attachments by X++

D365 FO - Send Email with attachments by X++

 

Go to System administration >> Setup >> Email >> System email templates

Create New template 


[ExtensionOf(classStr(PurchConfirmationJournalPost))]
final class PurchConfirmationJournalPost_TraxSCM_Extension
{
    public str  CCEmail, Esender;

    public void Sendmail()
    {
        try
        {
            str vendname = purchTable::find(this.vendPurchOrderJour.purchid).vendorName(); 
            str Purchid = this.vendPurchOrderJour.purchid;
            str sUserId;
             
                sUserId = curUserId();
                //Getting Email id from User Options --> Account --> Email Provider --> Email id
                str userEmail = SysUserInfo::find(sUserId, false).Email;
            
                var builder = new SysMailerMessageBuilder();
                builder.setBody(this.Email_Body());
                builder.setFrom(userEmail,Esender);
                builder.addcc(ccEmail);
               
                builder.addTo("[email protected]");
                builder.setSubject("subject string");
            
                //************
                System.IO.Stream     stream = this.generateReportStream(); // todo insert record reference
                if(stream)
                    builder.addAttachment(stream,vendname +" - "+Purchid+".pdf");
                var message = builder.getMessage();
               
              SysMailerFactory::getNonInteractiveMailer().sendNonInteractive(message);
            
        }
        catch (Exception::Error)
        {
            throw error("@SYS33567");
        }
    }


  public str Email_Body()
    {
        str vendname = purchTable::find(this.vendPurchOrderJour.purchid).vendorName();
        str Purchid = this.vendPurchOrderJour.purchid;

        str sub,tmp,Sender,frm,cc;
        [sub,tmp,Sender,frm,cc] =   this.getEmailTemplate('POConfirm','en-us');
        Esender  = sender;
        tmp = strReplace(tmp,'%vend%','%1');
        tmp = strReplace(tmp,'%purchid%','%2');
        str ret = strFmt(tmp,vendname,Purchid);

        return ret;
    }


 public System.IO.MemoryStream generateReportStream()
    {
        DocuRef                         addedRecord;
        VendPurchOrderJour              VendPurchOrderJour = this.vendPurchOrderJour;
        Filename                        fileName = "Report.pdf";
        PurchPurchaseOrderController controller = new PurchPurchaseOrderController();
        PurchPurchaseOrderContract    contract = new PurchPurchaseOrderContract();
        SRSPrintDestinationSettings     settings;
        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();
        System.IO.MemoryStream          stream;
        Args                            args = new Args();       ;

        args.record(VendPurchOrderJour);
        contract.parmRecordId(VendPurchOrderJour.RecId);
        controller.parmArgs(args);
        controller.parmReportName(ssrsReportStr(PurchPurchaseOrder, Report));
        controller.parmShowDialog(false);
        controller.parmReportContract().parmRdpContract(contract);
        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)
        {
            stream = new System.IO.MemoryStream(reportBytes);
        }
        return stream;
    }
  }


Published on:

Learn more
Sherif Fayed
Sherif Fayed

Share post:

Related posts

D365 Sending Email with Customer Account Statement SSRS report as attachment using X++

D365 Sending Email with Customer Account Statement SSRS report as attachment using X++ custTable _custTable;        &...

2 days ago

clicking link on info message X++ to Open form

 Message::AddAction() method can be used to embed an action within a message sent to the message bar. This method supports adding a singl...

3 days ago

Implement Periodic Data Export from D365FO to SFTP

This blog post describes how to implement various scenarios for periodic data export from D365FO to a file and uploading it to SFTP server.

14 days ago

[New Feature] Financial Consolidation Template in Microsoft Dynamics 365 Finance and Operations

πŸš€ New Feature Online Consolidation Template! πŸš€ This feature streamlines the financial consolidation process and enhances the user experience...

27 days ago

[New Feature] Bank Account Lifecycle Management in Microsoft Dynamics 365 Finance and Operations

πŸš€ New Feature: Workflow approvals for Bank master! πŸš€ Microsoft has recently added a new preview feature in Microsoft Dynamics 365 Finance an...

27 days ago

Business Performance Analytics in Microsoft Dynamics 365 Finance and Operations: Part-2

πŸ“’ Now Time to Business Performance Analytics in Microsoft Dynamics 365! Let’s dive into the various reports available in Business Perfo...

27 days ago

Business Performance Analytics in Microsoft Dynamics 365 Finance and Operations: Part-1

πŸ“’ Now Time to Business Performance Analytics in Microsoft Dynamics 365! While we focus on new developments around Copilot and AI, let’s...

27 days ago

Dual Currency Valuation/Reporting using Global Inventory Accounting in Microsoft Dynamics 365 Finance and Operations: Part-12

πŸ“’ Part-3: Now Time to Explore Global Inventory Accounting in Microsoft Dynamics 365! 🌍 Welcome to the next part of Global inventory accounti...

27 days ago

Configure Reporting for Global Inventory Accounting in Microsoft Dynamics 365 Finance and Operations: Part-11

πŸ“’ Part-2: Now Time to Explore Global Inventory Accounting in Microsoft Dynamics 365! 🌍 In the next part of our Global Inventory Accounting s...

27 days ago

Global Inventory Accounting in Microsoft Dynamics 365 Finance and Operations: Part-10

πŸ“’ Now Time to Explore Global Inventory Accounting in Microsoft Dynamics 365! 🌍 I’m excited to dive into one of the most powerful featu...

27 days ago
Stay up to date with latest Microsoft Dynamics 365 and Power Platform news!
* Yes, I agree to the privacy policy