Hello community,
I’m working on an integration where I create free text invoices from data pulled from a third-party source. After generating and posting the invoices, I generate a custom SSRS report (as a PDF) and attach it to each free text invoice. However, this final step is taking a significant amount of time, especially as I’m processing a large volume of invoices.
Could anyone suggest improvements to my code or offer alternative methods to speed up this part of the process? Below is the code I’m using to generate the PDF and attach it to the invoice.
Thank you in advance for your help!
public void generateAndAttachPDF(NPLOracleSupportHeader headerRecord)
{
CustInvoiceJour custInvoiceJour;
CustInvoiceTable custInvoiceTable;
PrintMgmtReportFormatName printMgmtReportFormatName;
FreeTextInvoiceContract freeTextInvoiceContract = new FreeTextInvoiceContract();
SrsReportRunController srsReportRunController;
SRSPrintDestinationSettings printerSettings;
System.Byte[] reportBytes;
SrsReportRunService srsReportRunService;
SRSProxy srsProxy;
Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[] parameterValueArray;
Map reportParametersMap;
str fileName = "";
SRSReportExecutionInfo executionInfo = new SRSReportExecutionInfo();
// Fetch the invoice journal and table records
select firstonly custInvoiceJour
where custInvoiceJour.InvoiceId == headerRecord.NPLOracleInvoiceIdDynamics
&& custInvoiceJour.NPLInvoiceId == headerRecord.NPLOracleInvoiceId;
if (!custInvoiceJour)
{
error("Invoice not found.");
return;
}
select firstonly custInvoiceTable
where custInvoiceTable.InvoiceId == custInvoiceJour.InvoiceId;
if (!custInvoiceTable)
{
error("Invoice table record not found.");
return;
}
// Set up the file name and print management report format
fileName = custInvoiceJour.InvoiceId + ".pdf";
printMgmtReportFormatName = ssrsReportStr(CustomFormatReport, Report);
// Initialize the report contract with the invoice record ID
freeTextInvoiceContract.parmCustInvoiceJourRecId(custInvoiceJour.RecId);
// Initialize the report controller once
srsReportRunController = new SrsReportRunController();
srsReportRunController.parmReportName(printMgmtReportFormatName);
srsReportRunController.parmExecutionMode(SysOperationExecutionMode::Asynchronous);
srsReportRunController.parmShowDialog(false);
srsReportRunController.parmLoadFromSysLastValue(false);
srsReportRunController.parmReportContract().parmRdpContract(freeTextInvoiceContract);
// Configure print settings to generate the PDF
printerSettings = srsReportRunController.parmReportContract().parmPrintSettings();
printerSettings.printMediumType(SRSPrintMediumType::File);
printerSettings.fileFormat(SRSReportFileFormat::PDF);
printerSettings.fileName(fileName);
printerSettings.overwriteFile(true);
try
{
// render the report
srsReportRunService = new SRSReportRunService();
srsReportRunController.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());
srsReportRunController.parmReportContract().parmReportExecutionInfo(executionInfo);
srsReportRunService.getReportDataContract(srsReportRunController.parmReportContract().parmReportName());
srsReportRunService.preRunReport(srsReportRunController.parmReportContract());
srsReportRunService.parmIsInBatch(true);
reportParametersMap = srsReportRunService.createParamMapFromContract(srsReportRunController.parmReportContract());
parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);
srsProxy = SRSProxy::constructWithConfiguration(srsReportRunController.parmReportContract().parmReportServerConfig());
// render report to byte array
reportBytes = srsProxy.renderReportToByteArray(
srsReportRunController.parmReportContract().parmReportPath(),
parameterValueArray,
printerSettings.fileFormat(),
printerSettings.deviceInfo()
);
// Attach the generated PDF to the invoice
if (reportBytes)
{
this.attachPDFToInvoice(custInvoiceTable, reportBytes, fileName);
}
else
{
error("Failed to render the report to a byte array.");
}
}
catch (Exception::Error)
{
error("An error occurred during the report generation.");
}
}
public void attachPDFToInvoice(CustInvoiceTable custInvoiceTable, System.Byte[] reportBytes, str fileName)
{
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(reportBytes);
DocuRef docuRef;
// Attach the PDF in a transaction
ttsBegin;
docuRef = DocumentManagement::attachFile(
custInvoiceTable.TableId,
custInvoiceTable.RecId,
custInvoiceTable.DataAreaId,
DocuType::typeFile(),
memoryStream,
fileName,
System.Web.MimeMapping::GetMimeMapping(fileName),
custInvoiceTable.InvoiceId
);
ttsCommit;
}