0% found this document useful (3 votes)
4K views

D365 - Generate A Report Through X++ and Save in A Local Folder

This article explains how to download a report automatically and save in a local folder. I have taken the example of sales invoice, the following code will download the sales order invoice automatically and save the PDF file in the specified path.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (3 votes)
4K views

D365 - Generate A Report Through X++ and Save in A Local Folder

This article explains how to download a report automatically and save in a local folder. I have taken the example of sales invoice, the following code will download the sales order invoice automatically and save the PDF file in the specified path.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

This article explains how to download a report automatically and save in a local folder.

I've taken the example of sales invoice, the following code will download the sales order
invoice automatically and save the PDF file in the specified path.

SrsReportRunController controller = new SrsReportRunController();


SalesInvoiceContract rdpContract = new SalesInvoiceContract();
SRSPrintDestinationSettings settings;
System.Byte[] reportBytes = new System.Byte[0]();
SRSProxy srsProxy;
SRSReportRunService srsReportRunService = new SrsReportRunService();
Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.Parameter
Value[] parameterValueArray;
Map reportParametersMap;
SRSReportExecutionInfo executionInfo = new SRSReportExecutionInfo();
;

if ( _custInvoiceJour)
{
if (!WinAPI::pathExists("C:\\Temp\\Invoices\\"))
WinAPI::createDirectoryPath("C:\\Temp\\Invoices\\");

filename
=strfmt('C:\\Temp\\Invoices\\%1%2',_custInvoiceJour.InvoiceId,".pdf");

controller.parmReportName(ssrsReportStr(SalesInvoic, ReportTH));
controller.parmShowDialog(false);

// Explicitly provide all required parameters


rdpContract.parmRecordId(_custInvoiceJour.RecId);
controller.parmReportContract().parmRdpContract(rdpContract);

// Change print settings as needed


settings = controller.parmReportContract().parmPrintSettings();
settings.printMediumType(SRSPrintMediumType::File);
settings.fileFormat(SRSReportFileFormat::PDF);
settings.overwriteFile(true);
settings.fileName(filename);
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().parmReportSe
rverConfig());
// Actual rendering to byte array
reportBytes =
srsproxy.renderReportToByteArray(controller.parmreportcontract().parmreportpath(),
parameterValueArray,
settings.fileFormat(),
settings.deviceinfo());

if (reportBytes)
{
System.IO.Stream stream = new
System.IO.MemoryStream(reportBytes);
var fileStream = new System.IO.FileStream(filename,
System.IO.FileMode::Create, System.IO.FileAccess::ReadWrite);
stream.CopyTo(fileStream);

You might also like