X++ code to generate SSRS Report Attachment in D365FO

 Below is the code snippet which helps to generate SSRS report as memory stream and then can be used it to send it through mail/upload in azure.


public static System.IO.Stream generateAttachment(PurchTable _purchTable)
    {
        PurchTable purchTable = PurchTable::find(_purchTable.PurchId);
        SrsReportRunController          ssrsController = new SrsReportRunController();
        ANI_PurchPurchaseOrderContract      Contract = new ANI_PurchPurchaseOrderContract();//contract class
        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();
        SRSPrintDestinationSettings     printerSettings;
        str                             ReportPath;
        SRSReportRUN       srsReportRun;
        System.String[] files;
        int             filecount;
        int             i;
        System.IO.Stream            stream;
        ReportPath = purchTable.PurchId+"abcd:PDF";
        ssrsController.parmReportName(ssrsReportStr(ANI_PurchPurchaseOrderReport, ANI_Report));//Report name
        ssrsController.parmExecutionMode(SysOperationExecutionMode::Synchronous);
        ssrsController.parmShowDialog(false);
        Contract.parmRecordId(purchTable.RecId);
        ssrsController.parmReportContract().parmRdpContract(Contract);
        //link the printer settings to the controller
        printerSettings = ssrsController.parmReportContract().parmPrintSettings();
        //print to pdf and always overwrite if the file exists
        printerSettings.overridePrintContractSettings(true);
        printerSettings.printMediumType(SRSPrintMediumType::Email);
        printerSettings.fileFormat(SRSReportFileFormat::PDF);
        printerSettings.overwriteFile(true);
        printerSettings.fileName(@ReportPath);
        
        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());
        System.IO.MemoryStream mstream = new System.IO.MemoryStream(reportBytes);
        return mstream;
    }

Popular posts from this blog

Send Purchase Orders to Vendor collaboration for Vendor Review in D365FO

Remove Prefix from a String in D365FO

Odata Patch operation using Postman