Generate and attach a SSRS report as PDF file in document attachment form through X++ code

               Sometimes we face this requirement to attach a SSRS reports directly on attachment form. Following is the code that helps you to achieve.

        str                          fileName;
        String10                     Extension;
        Args                         _args;
        SRSPrintDestinationSettings  printerSettings;
        System.Byte[]                reportBytes = new System.Byte[0]();
        SRSProxy                     srsProxy;
        SRSReportRunService          srsReportRunService = new SrsReportRunService();
                Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[]  parameterValueArray;
        Map                          reportParametersMap;
        str                          reportnamerun;
        SRSReportExecutionInfo       executionInfo = new SRSReportExecutionInfo();
        System.IO.Stream             stream = null;
        _args = new Args();
        filename = strfmt('%1%2',_rentAgreement.AgreementId,'.pdf');
 
AgreementController ssrsController = new AgreementController();
        _args.record(agreementUnit);
        ssrsController.parmReportName(reportname);
        ssrsController.parmArgs(_args);
        ssrsController.setRange(_args, ssrsController.parmReportContract().parmQueryContracts().lookup(ssrsController.getFirstQueryContractKey()));
        ssrsController.parmShowDialog(false);
        printerSettings = ssrsController.parmReportContract().parmPrintSettings();
        //print to pdf and always overwrite if the file exists
        printerSettings.printMediumType(SRSPrintMediumType::File);
        printerSettings.fileFormat(SRSReportFileFormat::PDF);
        printerSettings.overwriteFile(true);
        printerSettings.fileName(filename);
   
        ssrsController.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());
        ssrsController.parmReportContract().parmReportExecutionInfo(executionInfo);
   
        srsReportRunService.getReportDataContract(ssrsController.parmreportcontract().parmReportName());
        srsReportRunService.preRunReport(ssrsController.parmreportcontract());
        reportParametersMap = srsReportRunService.createParamMapFromContract(ssrsController.parmReportContract());
        parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);
   
        srsProxy = SRSProxy::constructWithConfiguration(ssrsController.parmReportContract().parmReportServerConfig());
        // Actual rendering to byte array
        reportBytes = srsproxy.renderReportToByteArray(ssrsController.parmreportcontract().parmreportpath(),
                                                                      parameterValueArray,
                                                                      printerSettings.fileFormat(),
                                                                      printerSettings.deviceinfo());
 
 
 
        stream         = new System.IO.MemoryStream(reportBytes);
        System.IO.MemoryStream Memorystream = stream;
        if (Memorystream && Memorystream.Length > 0)
        {
            Memorystream.Position = 0;
            DocumentManagement::attachFile(CustTable.TableId, CustTable.RecId, CustTable.RefCompany,enum2Symbol(enumNum(SRSPrintMediumType), SRSPrintMediumType::File), Memorystream, filename,
                                                 classstr (FileUploadTemporaryStorageStrategy), "Cust account");
        }
 
 

Comments

Popular posts from this blog

SSRS Report using Controller , Contract and RDP classes in D365

Exporting data to Excel through X++ code

COC for Form level method

How to pass the parameter from one form to another in Dynamic365

Multi Select Lookup in SSRS Report in D365