Skip to main content

Notifications

Dynamics 365 Community / Forums / Finance forum / Save SSRS report as pd...
Finance forum
Suggested answer

Save SSRS report as pdf in local for transfer order receive report

Posted on by 6
Hi All,

I need to download transfer order receive report and save it in local folder. I have written code like this , Iam facing issue in SRSProxy , its coming as null.
Can anyone please help me where i am doing wrong?
 

 
    
                str detailsId;
                str id, message, status, code, api_name, errorCode, codeDescription;
                str     jsonSerializedContract;
                FileName                    localPath;
       
     
                System.Byte[] reportBytes = new System.Byte[0]();
                SRSProxy srsProxy;
                SRSReportRunService srsReportRunService = new SrsReportRunService();
        
                Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[] parameterValueArray;
                Map reportParametersMap;
                SRSReportExecutionInfo executionInfo = new SRSReportExecutionInfo();
                str filename;
select inventTransferJour
inventTransferJour.recid == 5654675849;
                if (inventTransferJour)
                {
                    str ext = SRSPrintDestinationSettings::findFileNameType(SRSReportFileFormat::PDF, SRSImageFileFormat::BMP);
                    PrintMgmtReportFormatName printMgmtReportFormatName = PrintMgmtDocType::construct(PrintMgmtDocumentType::TransferOrderFiscalDoc_BR).getDefaultReportFormat();
                    if (!WinAPI::pathExists("C:\\Temp\\Invoices\\"))  WinAPI::createDirectoryPath("C:\\Temp\\Invoices\\");
                    filename=strfmt('C:\\Temp\\Invoices\\%1%2',inventTransferJour.VoucherId,".pdf");
                    InventTransferShipReceiveController InventTransferShipReceiveController = new InventTransferShipReceiveController();
                InventTransferShipReceiveContract InventTransferShipReceiveContract = new InventTransferShipReceiveContract();
                InventTransferShipReceiveContract.parmShowTaxInformation(false);
                    InventTransferShipReceiveController.parmReportName(ssrsReportStr(InventTransferReceive, Report));//printMgmtReportFormatName);
                    InventTransferShipReceiveController.parmExecutionMode(SysOperationExecutionMode::Synchronous);
                    InventTransferShipReceiveController.parmShowDialog(false);
                    // InventTransferShipReceiveController.parmReportContract().parmRdpContract(salesInvoiceContract);
                InventTransferShipReceiveController.parmReportContract().parmRdpContract(InventTransferShipReceiveContract);
                InventTransferShipReceiveController.parmLocalExportOptions();
                    Query           query = new Query(queryStr (InventTransfer)); // Query name.
                    QueryRun        qr;
                    QueryBuildRange qbr;
                    QueryBuildDataSource queryBuildDataSource = query.dataSourceTable(tableNum(InventTransferJour));
                    InventTransferUpdateType updateType = InventTransferUpdateType::Receive;
                    QueryBuildRange qbrUpdateType = SysQuery::findOrCreateRange(queryBuildDataSource, fieldNum(InventTransferJour, UpdateType));
                    qbrUpdateType.value(queryValue(updateType));
                    qbrUpdateType.status(RangeStatus::Locked);
                    QueryBuildRange qbrTransferId = SysQuery::findOrCreateRange(queryBuildDataSource, fieldNum(InventTransferJour, TransferId));
                    QueryBuildRange qbrVoucherId  = SysQuery::findOrCreateRange(queryBuildDataSource, fieldNum(InventTransferJour, VoucherId));
                    QueryBuildRange qbrTransDate  = SysQuery::findOrCreateRange(queryBuildDataSource, fieldNum(InventTransferJour, TransDate));
                    if (inventTransferJour)
                    {
              
                        qbrTransferId.value(queryValue(inventTransferJour.TransferId));
                        qbrVoucherId.value(queryValue(inventTransferJour.VoucherId));
                        qbrTransDate.value(queryValue(inventTransferJour.TransDate));
                    }
                    InventTransferShipReceiveController.setRanges(query);
        
                    // printerSettings.parmFileName(@'C:\Temp\'+custInvoiceJour.InvoiceId + ext);
                    SRSPrintDestinationSettings printerSettings = InventTransferShipReceiveController.parmReportContract().parmPrintSettings();
                    printerSettings.printMediumType(SRSPrintMediumType::File);
                    printerSettings.fileFormat(SRSReportFileFormat::PDF);
                    printerSettings.overwriteFile(true);
                    printerSettings.fileName(filename);
                    InventTransferShipReceiveController.startOperation();
                    srsReportRunService.getReportDataContract( InventTransferShipReceiveController.parmreportcontract().parmReportName());
                    srsReportRunService.preRunReport(InventTransferShipReceiveController.parmreportcontract());
                    reportParametersMap= srsReportRunService.createParamMapFromContract(InventTransferShipReceiveController.parmReportContract());
                    parameterValueArray =SrsReportRunUtil::getParameterValueArray(reportParametersMap);
          
                    srsProxy =SRSProxy::constructWithConfiguration(InventTransferShipReceiveController.parmReportContract().parmReportServerConfig());
            
                    // Actual rendering to byte array
                    reportBytes =srsProxy.renderReportToByteArray(InventTransferShipReceiveController.parmreportcontract().parmreportpath(), parameterValueArray, printerSettings.fileFormat(), printerSettings.deviceinfo());
            
                    using (var stream = new System.IO.MemoryStream(reportBytes))
                    {
                        using (var fileStream = new System.IO.FileStream(filename,System.IO.FileMode::Create, System.IO.FileAccess::ReadWrite))
                    {
                        stream.CopyTo(fileStream);
                    }
                    
                    }
          
               
             
            
    
              
        }
 
  • Suggested answer
    Anton Venter Profile Picture
    Anton Venter 18,646 Super User 2024 Season 2 on at
    Save SSRS report as pdf in local for transfer order receive report
    Rather use the Stream/MemoryStream class in your approach. The PDF document is contained in the Stream/MemoryStream object and you can send the PDF from this object. You don't have to download the PDF file to a local folder to be able to send it to an API. I also recommend saving the PDF document as an attachment or some other location in F&O if you want to access it again.
  • PJ-03101359-0 Profile Picture
    PJ-03101359-0 6 on at
    Save SSRS report as pdf in local for transfer order receive report
    Hi Martin,

    thanks for your reply.

    I need to send the receive transfer report file to the api, so i am dowloding the file and store somewhere and read the file and send the file to the API.

    So the first step i need to download the transfer order receive report.

    can you please correct my code, i done the same way to download the sales invoice report its working.
    But transfer order i am not able to download the file.

    Getting object null reference error in ssproxy.

    can you please guide what i am doing wrong in the report.

    For sales invoice download i have written this code
        str ext = SRSPrintDestinationSettings::findFileNameType(SRSReportFileFormat::PDF, SRSImageFileFormat::BMP);
        PrintMgmtReportFormatName printMgmtReportFormatName = PrintMgmtDocType::construct(PrintMgmtDocumentType::SalesOrderInvoice).getDefaultReportFormat();
    
        SalesInvoiceContract salesInvoiceContract = new SalesInvoiceContract();
        salesInvoiceContract.parmRecordId(_custinvoicejour.RecId);
    
        SrsReportRunController  srsReportRunController = new SrsReportRunController();
        if (!WinAPI::pathExists("C:\\Temp\\Invoices\\"))  WinAPI::createDirectoryPath("C:\\Temp\\Invoices\\");
        filename=strfmt('C:\\Temp\\Invoices\\%1%2',_custinvoicejour.InvoiceId,".pdf");
        srsReportRunController.parmReportName(printMgmtReportFormatName);
        srsReportRunController.parmExecutionMode(SysOperationExecutionMode::Synchronous);
        srsReportRunController.parmShowDialog(false);
        srsReportRunController.parmReportContract().parmRdpContract(salesInvoiceContract);
        srsReportRunController.parmLocalExportOptions();
    
               
        // printerSettings.parmFileName(@'C:\Temp\'+custInvoiceJour.InvoiceId + ext);
        SRSPrintDestinationSettings printerSettings = srsReportRunController.parmReportContract().parmPrintSettings();
        printerSettings.printMediumType(SRSPrintMediumType::File);
        printerSettings.fileFormat(SRSReportFileFormat::PDF);
        printerSettings.overwriteFile(true);
        printerSettings.fileName(filename);
        srsReportRunController.startOperation();
        //srsReportRunController.startOperation();
        srsReportRunService.getReportDataContract( srsReportRunController.parmreportcontract().parmReportName());
        srsReportRunService.preRunReport(srsReportRunController.parmreportcontract());
        reportParametersMap= srsReportRunService.createParamMapFromContract(srsReportRunController.parmReportContract());
        parameterValueArray =SrsReportRunUtil::getParameterValueArray(reportParametersMap);
              
        srsProxy =SRSProxy::constructWithConfiguration(srsReportRunController.parmReportContract().parmReportServerConfig());
    
        // Actual rendering to byte array
        reportBytes =srsProxy.renderReportToByteArray(srsReportRunController.parmreportcontract().parmreportpath(), parameterValueArray, printerSettings.fileFormat(), printerSettings.deviceinfo());
    
        if (reportBytes)
        {
            using (var stream = new System.IO.MemoryStream(reportBytes))
    
            {
                using (var fileStream = new System.IO.FileStream(filename,System.IO.FileMode::Create, System.IO.FileAccess::ReadWrite))
                {
                    stream.CopyTo(fileStream);
                    //stream.Close();
                    //stream.Dispose();
                    //stream.Flush();
               
                }
            }
           
           /// var fileStream = new System.IO.FileStream(filename,System.IO.FileMode::Create, System.IO.FileAccess::ReadWrite);
            
        }
              
    
    }

    Similar way i want to download transfer order receive pdf download, but its not working, can you please guide what i done wrong.

    Thanks
     
  • Martin Dráb Profile Picture
    Martin Dráb 229,173 Most Valuable Professional on at
    Save SSRS report as pdf in local for transfer order receive report
    The whole idea storing a file in a local folder is wrong. We're talking about a cloud system, therefore "local" means "somewhere in cloud". Technical, there is a bunch of application servers and you save the file on one of them (if it allows you to write to C:\Temp at all). You can't access the file directly and if you try to do it from code, your code may run on a different server, where the file doesn't exist.
     
    Please explain your actual business requirement, so we can suggest a solution that can actually work. In general, you may want to save the file in a cloud storage (and let users download it when needed) and/or download it to a local system by something like Recurring Integrations Scheduler.
     
    By the way, you should simplify your code before sharing it. For example, all the query ranges or unused variables aren't related to your question and just complicate understanding of the code. You should also use methods to split your code to smaller, named pieces.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,867 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,173 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans