how to generate SSRS Report and store it in attachment table(DocuRef)

 class RunReportToStreamTest

{
    public static void main(Args _args)
    {
        DocuRef                         addedRecord;
        ProdTable                       prodTable = ProdTable::find('P00017 3');
        Filename                        fileName = "AbcTest.pdf";
        YourReportController 	controller = new YourReportController();
        YourReportContract   	contract = new YourReportContract();
        SRSPrintDestinationSettings     settings;
        Array                           arrayFiles;
        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();
        ;

        _args = new Args();
        _args.record(prodTable);
        // Provide all the parameters to a contract
        contract.parmProdId('P000173');
        contract.parmNumberOfLabels(1);
	 // Provide details to controller and add contract		
        controller.parmArgs(_args);
        controller.parmReportName(ssrsReportStr(YourReportName, DesignName));
        controller.parmShowDialog(false);
        controller.parmLoadFromSysLastValue(false);
        controller.parmReportContract().parmRdpContract(contract);
        // Provide printer settings
        settings = controller.parmReportContract().parmPrintSettings();
        settings.printMediumType(SRSPrintMediumType::File);
        settings.fileName(fileName);
        settings.fileFormat(SRSReportFileFormat::PDF);

        // Below is a part of code responsible for rendering the report
        controller.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());
        controller.parmReportContract().parmReportExecutionInfo(executionInfo);

        srsReportRunService.getReportDataContract(controller.parmreportcontract().parmReportName());
        srsReportRunService.preRunReport(controller.parmreportcontract());
        reportParametersMap = srsReportRunService.createParamMapFromContract(controller.parmReportContract());
        parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);

        srsProxy = SRSProxy::constructWithConfiguration(controller.parmReportContract().parmReportServerConfig());
	 // Actual rendering to byte array
        reportBytes = srsproxy.renderReportToByteArray(controller.parmreportcontract().parmreportpath(),
                                              parameterValueArray,
                                              settings.fileFormat(),
                                              settings.deviceinfo());

        if (reportBytes)
        {
		// Converting byte array to memory stream
            System.IO.MemoryStream stream = new System.IO.MemoryStream(reportBytes);

            // Upload file to temp storage and direct the browser to the file URL
            File::SendFileToUser(stream, settings.parmFileName());

            stream.Position = 0;
            str fileContentType = System.Web.MimeMapping::GetMimeMapping(fileName);
            // Attach the file to a record using stream object
            addedRecord = DocumentManagement::attachFile(prodTable.TableId,prodTable.RecId,prodTable.DataAreaId, 'File', stream, fileName, fileContentType,"PDF file attached");
        }


// You can also convert the report Bytes into an xpp BinData object if needed
        container binData;
        Binary binaryData;
        System.IO.MemoryStream mstream = new System.IO.MemoryStream(reportBytes);
        binaryData = Binary::constructFromMemoryStream(mstream);
        if(binaryData)
        {
            binData = binaryData.getContainer();
        }

    }

}

Comments

Popular posts from this blog

How to Bypass Next in COC in D365 X++

create movement journal through code in d365 FO x++