Header Ads Widget

📄 Generate and Attach an SSRS Report as PDF to the Document Attachment Form in D365FO Using X++

 In many business cases, you might want to automatically generate a PDF of a report and attach it directly to the document handling form (also known as the attachment form) in Dynamics 365 Finance and Operations. This can be useful for storing agreement copies, invoices, delivery notes, or any other document for reference or compliance.

Here’s how to programmatically generate a report and attach it as a PDF using X++.


X++ Code to Generate and Attach PDF to Attachments:

x++
public static void generateAndAttachReportToDocHandling(SalesTable _salesTable, str _reportName) { Args argsObj; SRSPrintDestinationSettings destSettings; SRSProxy proxyObj; SRSReportRunService reportService = new SRSReportRunService(); System.IO.Stream memStream; SRSReportExecutionInfo execInfo = new SRSReportExecutionInfo(); System.Byte[] pdfBytes; str outputFileName; Map paramMap; Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[] paramArray; outputFileName = strFmt('%1_%2.pdf', _salesTable.SalesId, systemDateGet()); argsObj = new Args(); argsObj.record(_salesTable); CustomReportController rptController = new CustomReportController(); rptController.parmReportName(_reportName); rptController.parmArgs(argsObj); rptController.setRange(argsObj, rptController.parmReportContract().parmQueryContracts().lookup(rptController.getFirstQueryContractKey())); rptController.parmShowDialog(false); destSettings = rptController.parmReportContract().parmPrintSettings(); destSettings.printMediumType(SRSPrintMediumType::File); destSettings.fileFormat(SRSReportFileFormat::PDF); destSettings.overwriteFile(true); destSettings.fileName(outputFileName); rptController.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration()); rptController.parmReportContract().parmReportExecutionInfo(execInfo); reportService.getReportDataContract(rptController.parmReportContract().parmReportName()); reportService.preRunReport(rptController.parmReportContract()); paramMap = reportService.createParamMapFromContract(rptController.parmReportContract()); paramArray = SrsReportRunUtil::getParameterValueArray(paramMap); proxyObj = SRSProxy::constructWithConfiguration(rptController.parmReportContract().parmReportServerConfig()); pdfBytes = proxyObj.renderReportToByteArray( rptController.parmReportContract().parmReportPath(), paramArray, destSettings.fileFormat(), destSettings.deviceInfo() ); memStream = new System.IO.MemoryStream(pdfBytes); if (memStream && memStream.Length > 0) { memStream.Position = 0; DocumentManagement::attachFile( _salesTable.TableId, _salesTable.RecId, _salesTable.dataAreaId, enum2Symbol(enumNum(SRSPrintMediumType), SRSPrintMediumType::File), memStream, outputFileName, classStr(FileUploadTemporaryStorageStrategy), "Sales Order Report" ); } }

🔍 Use Case Example:

Let's say you want to automatically generate a Sales Order confirmation PDF every time an order is confirmed, and attach it to the record. By calling this method from your custom logic during workflow steps or post-confirmation event handlers, the document will always be available for audit or reference.


🔧 Important Considerations:

  • Replace CustomReportController with your actual report controller class.

  • You can reuse this logic with any SSRS report—just update the data source record and report name.

  • The file name is dynamically generated with the current date to ensure uniqueness.


🧠 Wrap-up:

Attaching SSRS reports programmatically improves process automation and document traceability in D365FO. Whether you're handling agreements, sales, or expense reports, this method saves users from downloading and uploading files manually.


Post a Comment

0 Comments