web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Saved packingslip contains wrong data

(0) ShareShare
ReportReport
Posted on by 174
Hello, 
I'm trying to make custom extension that saves packingslips. It works somehow but the data is not from the packing slip I do pass the recId.
Sometimes I get just the template without data or data of another packingslip( the id is always near the one I try to save).
As I debugged the code the contract always pointed the right parmRecordId yet the saved file contains data from a random one.
Any idea about it?
 
 private void savePackingSlipToPdfFile(RecId _recId)        {            if (_recId)        {            custPackingSlipJour packingslip = custPackingSlipJour::findRecId(_recId);            PrintMgmtReportFormatName printMgmtReportFormatName = PrintMgmtDocType::construct(PrintMgmtDocumentType::SalesOrderPackingSlip).getDefaultReportFormat();            SalesPackingSlipContract SalesPackingSlipContract = new SalesPackingSlipContract();            SalesPackingSlipContract.parmRecordId(_recId);            SalesPackingSlipContract.parmTableId(tableNum(custPackingSlipJour));            SrsReportRunController srsReportRunController = new SrsReportRunController();            srsReportRunController.parmReportName(printMgmtReportFormatName);            srsReportRunController.parmExecutionMode(SysOperationExecutionMode::Synchronous);            srsReportRunController.parmShowDialog(false);            var args = new Args();            args.parmEnum(PrintCopyOriginal::OriginalPrint);            args.record(packingslip);            srsReportRunController.parmArgs(args);            srsReportRunController.parmReportContract().parmRdpContract(SalesPackingSlipContract);            srsReportRunController.parmReportContract().parmReportExecutionInfo(new SRSReportExecutionInfo());            srsReportRunController.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());            srsReportRunController.parmReportContract().parmRdlContract().parmLabelLanguageId(packingslip.languageId);            str documentTitle = literalStr(/@SYS11549/);            documentTitle = SysLabel::labelId2String2(documentTitle, srsReportRunController.parmReportContract().parmRdlContract().parmLanguageId(packingslip.languageId));            SalesPackingSlipContract.parmDocumentTitle(documentTitle);            SRSPrintDestinationSettings printerSettings = srsReportRunController.parmReportContract().parmPrintSettings();            printerSettings.printMediumType(SRSPrintMediumType::File);            printerSettings.fileFormat(SRSReportFileFormat::PDF);            printerSettings.parmFileName(filePath + fileName);            printerSettings.overwriteFile(true);            SRSReportRunService srsReportRunService = new SrsReportRunService();            srsReportRunService.getReportDataContract(srsReportRunController.parmReportContract().parmReportName());            srsReportRunService.preRunReport(srsReportRunController.parmReportContract());            Map reportParametersMap = srsReportRunService.createParamMapFromContract(srsReportRunController.parmReportContract());            Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[]  parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);            SRSProxy srsProxy = SRSProxy::constructWithConfiguration(srsReportRunController.parmReportContract().parmReportServerConfig());            System.Byte[] reportBytes = srsproxy.renderReportToByteArray(srsReportRunController.parmreportcontract().parmreportpath(), parameterValueArray, printerSettings.fileFormat(), printerSettings.deviceinfo());            if (reportBytes)            {                System.IO.Stream stream = new System.IO.MemoryStream(reportBytes);                System.IO.FileStream fileStream = new System.IO.FileStream(filePath + fileName, System.IO.FileMode::Create, System.IO.FileAccess::ReadWrite);                stream.CopyTo(fileStream);                fileStream.Close();            }        }    }
 
I have the same question (0)
  • Martin Dráb Profile Picture
    238,745 Most Valuable Professional on at
    First of all, let me re-post your code so we can read it:
    private void savePackingSlipToPdfFile(RecId _recId)
    {
        if (_recId)
        {
            CustPackingSlipJour packingslip = custPackingSlipJour::findRecId(_recId);
            PrintMgmtReportFormatName printMgmtReportFormatName = PrintMgmtDocType::construct(PrintMgmtDocumentType::SalesOrderPackingSlip).getDefaultReportFormat();
            SalesPackingSlipContract SalesPackingSlipContract = new SalesPackingSlipContract();
            SalesPackingSlipContract.parmRecordId(packingslip.RecId);
            SalesPackingSlipContract.parmTableId(tableNum(custPackingSlipJour));
            SrsReportRunController srsReportRunController = new SrsReportRunController();
            srsReportRunController.parmReportName(printMgmtReportFormatName);
            srsReportRunController.parmExecutionMode(SysOperationExecutionMode::Synchronous);
            srsReportRunController.parmShowDialog(false);
            
            var args = new Args();
            args.parmEnum(PrintCopyOriginal::OriginalPrint);
            args.record(packingslip);
            srsReportRunController.parmArgs(args);
            srsReportRunController.parmReportContract().parmRdpContract(SalesPackingSlipContract);
            srsReportRunController.parmReportContract().parmReportExecutionInfo(new SRSReportExecutionInfo());
            srsReportRunController.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());
            srsReportRunController.parmReportContract().parmRdlContract().parmLabelLanguageId(packingslip.languageId);
            
            str documentTitle = literalStr(/@SYS11549/);
            documentTitle = SysLabel::labelId2String2(documentTitle, srsReportRunController.parmReportContract().parmRdlContract().parmLanguageId(packingslip.languageId));
            SalesPackingSlipContract.parmDocumentTitle(documentTitle);
            SRSPrintDestinationSettings printerSettings = srsReportRunController.parmReportContract().parmPrintSettings();
            printerSettings.printMediumType(SRSPrintMediumType::File);
            printerSettings.fileFormat(SRSReportFileFormat::PDF);
            printerSettings.parmFileName(filePath + fileName);
            printerSettings.overwriteFile(true);
            SRSReportRunService srsReportRunService = new SrsReportRunService();
            srsReportRunService.getReportDataContract(srsReportRunController.parmReportContract().parmReportName());
            srsReportRunService.preRunReport(srsReportRunController.parmReportContract());
            Map reportParametersMap = srsReportRunService.createParamMapFromContract(srsReportRunController.parmReportContract());
            Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[]  parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);
            SRSProxy srsProxy = SRSProxy::constructWithConfiguration(srsReportRunController.parmReportContract().parmReportServerConfig());
            System.Byte[] reportBytes = srsproxy.renderReportToByteArray(srsReportRunController.parmreportcontract().parmreportpath(), parameterValueArray, printerSettings.fileFormat(), printerSettings.deviceinfo());
            
            if (reportBytes)
            {
                System.IO.Stream stream = new System.IO.MemoryStream(reportBytes);
                System.IO.FileStream fileStream = new System.IO.FileStream(filePath + fileName, System.IO.FileMode::Create, System.IO.FileAccess::ReadWrite);
                stream.CopyTo(fileStream);
                fileStream.Close();
            }
        }
    }
     
     
  • Martin Dráb Profile Picture
    238,745 Most Valuable Professional on at
    You're trying to save the file on disk, which doesn't make a good sense. You write the file on disk on AOS1, but when you try to read the file, code will execute on AOS2 and tell you that no such a file exists. This is not reasonable design for a cloud solution.
     
    By the way, I tried to structure your code better to make it easier to understand:
    private void savePackingSlipToPdfFile(RecId _recId)
    {
        if (!_recId)
        {
            return;
        }
        
        CustPackingSlipJour packingSlip = custPackingSlipJour::findRecId(_recId);
        SrsReportRunController reportController = this.createReportController(packingSlip);
        SrsReportDataContract reportContract = reportController.parmReportContract(reportController, packingSlip);
        
        SRSPrintDestinationSettings printerSettings = reportContract.parmPrintSettings();
        printerSettings.printMediumType(SRSPrintMediumType::File);
        printerSettings.fileFormat(SRSReportFileFormat::PDF);
        printerSettings.parmFileName(filePath + fileName);
        printerSettings.overwriteFile(true);
        
        var parameterValueArray = this.createParameters(reportContract);
        
        SRSProxy srsProxy = SRSProxy::constructWithConfiguration(reportContract.parmReportServerConfig());
        System.Byte[] reportBytes = srsproxy.renderReportToByteArray(
            reportContract.parmReportpath(),
            parameterValueArray,
            printerSettings.fileFormat(),
            printerSettings.deviceinfo());
        
        if (reportBytes)
        {
            System.IO.Stream stream = new System.IO.MemoryStream(reportBytes);
            System.IO.FileStream fileStream = new System.IO.FileStream(filePath + fileName, System.IO.FileMode::Create, System.IO.FileAccess::ReadWrite);
            stream.CopyTo(fileStream);
            fileStream.Close();
        }
    }
    
    private SrsReportRunController createReportController(CustPackingSlipJour _packingSlip)
    {
        PrintMgmtReportFormatName printMgmtReportFormatName = PrintMgmtDocType::construct(PrintMgmtDocumentType::SalesOrderPackingSlip).getDefaultReportFormat();
        
        SrsReportRunController reportController = new SrsReportRunController();
        reportController.parmReportName(printMgmtReportFormatName);
        reportController.parmExecutionMode(SysOperationExecutionMode::Synchronous);
        reportController.parmShowDialog(false);
        
        Args args = new Args();
        args.parmEnum(PrintCopyOriginal::OriginalPrint);
        args.record(_packingSlip);
        
        reportController.parmArgs(args);
        
        return reportController;
    }
    
    private SrsReportDataContract createReportContract(SrsReportRunController _reportController, CustPackingSlipJour _packingSlip)
    {
        SrsReportDataContract reportContract = _reportController.parmReportContract();
        
        reportContract.parmRdpContract(this.createDataContract(_packingSlip));
        reportContract.parmReportExecutionInfo(new SRSReportExecutionInfo());
        reportContract.parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());
        reportContract.parmRdlContract().parmLabelLanguageId(packingSlip.languageId);
        
        return reportContract;
    }
    
    private SalesPackingSlipContract createDataContract(CustPackingSlipJour _packingSlip)
    {
        SalesPackingSlipContract salesPackingSlipContract = new SalesPackingSlipContract();
        salesPackingSlipContract.parmRecordId(_packingSlip.RecId);
        salesPackingSlipContract.parmTableId(tableNum(CustPackingSlipJour));
        
        str documentTitle = SysLabel::labelId2String2(literalStr("@SYS11549"), packingSlip.languageId));
        salesPackingSlipContract.parmDocumentTitle(documentTitle);
        
        return salesPackingSlipContract;
    }
    
    private Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[] createParameters(SrsReportDataContract _contract)
    {
        SRSReportRunService srsReportRunService = new SrsReportRunService();
        srsReportRunService.getReportDataContract(_contract.parmReportName());
        srsReportRunService.preRunReport(_contract);
    
        Map reportParametersMap = srsReportRunService.createParamMapFromContract(_contract);
        return SrsReportRunUtil::getParameterValueArray(reportParametersMap);
    }
    (Excuse me if there are bugs, I made the change without a compiler.)
     
    I believe that some parts of your code has no effect at all. For example, you set the execution mode and Args of the controller, but you never actually execute it. You use the controller for only one purpose: getting the report contract.
  • RadekM Profile Picture
    174 on at
    I do save the files on a different server since we need to have a copy of every document in our archive storage. We work on on-premise version of D365 FO.
    I do not need to read them I just need a saved copy of the file.
    I do have almost the same code to store invoices and yet only the packingslips save like I mentioned in the topic.
  • Verified answer
    RadekM Profile Picture
    174 on at
    I did find where the problem is... The Recid I'm passing along with Tableid of CustPackingSlipJour is actually using the Recid with CustPackingSlipVersion...
    Now the case is to figure how to change it to use CustPackingSlipJour.

    @edit solved by changing RecordId to

    SalesPackingSlipContract.parmRecordId(CustPackingSlipVersion::findLatest(_recId).RecId);

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.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 510 Super User 2026 Season 1

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 399

#3
Adis Profile Picture

Adis 251 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans