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

Practical Hints for Technical Management of D365FO Go-Live

In this post, I share practical insights from my experience managing the technical side of Go-Live for D365FO projects, focusing on key activi...

26 days ago

D365FO Integration: Event-Based Exports to External Web Services

How to implement robust, efficient integrations between Dynamics 365 Finance and Operations and external Web Services. This post covers design...

1 month ago

Physical cost inclusion for Weighted Average Valuation in Microsoft Dynamics 365 Finance and Operations: Part-13

What is physical cost in Dynamics 365 F&O? Anything which has been received or shipped but not invoiced is considered as physical cost for...

1 month ago

Product bundles in Microsoft Dynamics 365 Finance and Operations

Product bundle was first introduced in module revenue recognition but as we all know the there is new modules which has been introduced and wi...

1 month ago

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;        &...

4 months 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...

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