public void generateAndAttachSalaryRegisterExcel(ISPAccountingPeriod accountingPeriod, RefRecId headerRecId) { SrsReportRunController controller = new SrsReportRunController(); SalaryRegisterByEmpContract contract = new SalaryRegisterByEmpContract(); SRSPrintDestinationSettings settings; SrsReportDataContract reportDataContract; SRSReportRunService srsReportRunService = new SrsReportRunService(); DocuRef docuRef; DocuActionArchive archive; SRSProxy srsProxy; Map reportParamMap; Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[] parameterValueArray; str reportPath = ssrsReportStr(PayRegisterByEmp, Report); str baseFolder,folderName,folderPath,tempFilePath,fileName; TransDate currentDate; System.DateTime sysDateTime; baseFolder = @'C:\Temp'; folderName = 'Emp Salary Register'; folderPath = baseFolder + '\\' + folderName; // Create folder if it doesn't exist System.IO.Directory::CreateDirectory(folderPath); currentDate = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone()); sysDateTime = DateTimeUtil::newDateTime(currentDate, DateTimeUtil::getTimeNow(DateTimeUtil::getUserPreferredTimeZone())); fileName = strFmt("SalaryRegister_%1.xlsx", sysDateTime.ToString("ddMMyyyyHHmmss")); tempFilePath = folderPath + '\\' + fileName; // Create print settings settings = new SRSPrintDestinationSettings(); settings.printMediumType(SRSPrintMediumType::File); settings.fileFormat(SRSReportFileFormat::Excel); settings.fileName(tempFilePath); // Setup controller controller = new SrsReportRunController(); controller.parmReportName(reportPath); controller.parmShowDialog(false); controller.parmLoadFromSysLastValue(false); // Set contract contract = new ARPayrollRunSalaryRegisterByEmpContract(); contract.parmAccountingPeriod(accountingPeriod); // Assign contracts reportDataContract = controller.parmReportContract(); reportDataContract.parmRdpContract(contract); reportDataContract.parmPrintSettings(settings); reportDataContract.parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration()); reportDataContract.parmReportExecutionInfo(new SRSReportExecutionInfo()); // Required step: Get data contract from SSRS runtime srsReportRunService.getReportDataContract(reportPath); // REQUIRED: preRunReport – run validation / setup srsReportRunService.preRunReport(reportDataContract); reportParamMap = srsReportRunService.createParamMapFromContract(reportDataContract); parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParamMap); // Render report to byte[] srsProxy = SRSProxy::constructWithConfiguration(SRSConfiguration::getDefaultServerConfiguration()); System.Byte[] reportBytes = srsProxy.renderReportToByteArray( controller.parmReportContract().parmReportPath(), parameterValueArray, settings.fileFormat(), settings.deviceinfo()); // Save reportBytes to file manually System.IO.File::WriteAllBytes(tempFilePath, reportBytes); System.IO.Stream stream = new System.IO.MemoryStream(reportBytes); this.attachDocumentToHeaderFromStream( stream, headerRecId, tableNum(EmpSalaryRegister), fileName ); }
public void attachDocumentToHeaderFromStream( System.IO.Stream stream, RefRecId headerRecId, TableId headerTableId, str fileName, str docuTypeId = 'File', // Optional: Specific docuType ID, must exist str mimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') // Excel MIME type { DocuRef docuRef; DocuType docuType; if (!stream || headerRecId == 0 || headerTableId == 0) { throw error("Invalid parameters for document attachment."); } // Validate document type docuType = DocuType::find(docuTypeId); if (!docuType.RecId) { throw error(strFmt("Document type '%1' not found.", docuTypeId)); } // Attach the file using standard API docuRef = DocumentManagement::attachFile( headerTableId, // TableId of the record headerRecId, // RecId of the record 'dat', // DataAreaId (current company) enum2Symbol(enumNum(SRSPrintMediumType), SRSPrintMediumType::File), // Print medium type stream, // File stream fileName, // File name mimeType, // MIME type fileName); // Description (optional) if (docuRef.RecId) { info(strFmt("Document '%1' successfully attached to record %2.", fileName, headerRecId)); } else { warning(strFmt("Failed to attach document '%1' to record %2.", fileName, headerRecId)); } }
Hi Dinesh 👋
Thanks for sharing your code and question!
From your description, it seems the file is being successfully generated and stored, and it appears in the document history, but not in the visible attachments section of the form. This usually indicates a mismatch in how the document is being linked or displayed.
✅ Here are a few things to check and try:
Check the DocuType configuration
DocuType
File
Attachment
Verify the DataAreaId
DataAreaId
attachDocumentToHeaderFromStream
'dat'
CompanyInfo::currentDataAreaId()
curExt()
Ensure the form is configured to show attachments
Confirm the record context
headerRecId
headerTableId
Debug with DocuRef viewer
DocuRef
✅ Please mark this reply as helpful if it answered your question. Best regards! 👋
Under review
Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
CA Neeraj Kumar 1,922
André Arnaud de Cal... 835 Super User 2025 Season 2