Run report and send thru email attachment – Dynamics 365 for Finance and Operations

In Dynamics 365 we use SysMailerFactory class to send an email thru X++ code,

Below is the code to send an email with the attachment. You can comment the attachment code if you want only to send an email thru X++

//Define variables

Filename fileName = “AbcTest.pdf”;
SrsReportRunController controller = new SrsReportRunController();
ReportContractClass contract = new ReportContractClass();
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();

//Pass contract variables
contract.parm1(); //Some contract class method for parameter
contract.parm2(); //Some contract class method for parameter

//Controller class for the report name and set contract class for controller

controller.parmReportName(ssrsReportStr(); // Report name and design

// Provide printer settings
settings = controller.parmReportContract().parmPrintSettings();

// Below is a part of code responsible for rendering the report

reportParametersMap = srsReportRunService.createParamMapFromContract(controller.parmReportContract());
parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);

srsProxy = SRSProxy::constructWithConfiguration(controller.parmReportContract().parmReportServerConfig());
// Actual rendering to byte array
reportBytes = srsproxy.renderReportToByteArray(controller.parmreportcontract().parmreportpath(),parameterValueArray,settings.fileFormat(),settings.deviceinfo());

// You can also convert the report Bytes into an xpp BinData object if needed
container binData;
Binary binaryData;
System.IO.MemoryStream mstream = new System.IO.MemoryStream(reportBytes);
binaryData = Binary::constructFromMemoryStream(mstream);
binData = binaryData.getContainer();

System.Byte[] binData1;
System.IO.Stream stream1;

// Turn the Bytes into a stream
for(int i = 0; i < conLen(binData); i++)
binData1 = conPeek(binData,i+1);
stream1 = new System.IO.MemoryStream(binData1);

var mail = SysMailerFactory::getNonInteractiveMailer();
var messageBuilder = new SysMailerMessageBuilder();
// for (i = 1; i <= conLen(_notifyCon); i++)
// {
// notifyEmailsStr = conPeek(_notifyCon, i);
// select firstonly RecId, Email from sysUser where sysUser.Id == notifyEmailsStr;
// if (sysUser.RecId && sysUser.Email != ”)
.setFrom(‘from email address’)
.addTo(‘to email address’)
.setSubject(‘Email Subject’)
.setBody(‘Email Body’);

if (stream1 != null)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.