Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Answered

Saved packingslip contains wrong data

(0) ShareShare
ReportReport
Posted on by 76
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();            }        }    }
 
  • Verified answer
    RadekM Profile Picture
    RadekM 76 on at
    Saved packingslip contains wrong data
    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);
  • RadekM Profile Picture
    RadekM 76 on at
    Saved packingslip contains wrong data
    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.
  • Martin Dráb Profile Picture
    Martin Dráb 230,868 Most Valuable Professional on at
    Saved packingslip contains wrong data
    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.
  • Martin Dráb Profile Picture
    Martin Dráb 230,868 Most Valuable Professional on at
    Saved packingslip contains wrong data
    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();
            }
        }
    }
     
     

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,031 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,868 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans