Thursday, December 3, 2020

Save D365 FO cloud environment Document print pdf file in Sharepoint location through X++ batch job

Hi folks,

Here is another blog that will be helpful in case where Dynamics 365 Finance and Operations cloud environment Document print pdf file needs to be saved to Sharepoint location that can be shared with any other external application for integration purpose. 

I am using Sales Quotations print pdf as an example in this post. You can follow the same practice for Purchase order, Sales Invoices or any other Document print pdf as per your requirement.

Create a class that should be inherited from RunBaseBatch framework class in order to execute batch jobs

Below is the code snippet of batch job that will save Document print pdf file in Sharepoint location on periodic basis from Dynamics 365 Finance and Operations cloud environment. 

class SalesQuotationsIntegration extends RunBaseBatch

{

}

Below method can be used to save multiple Document print pdf file in Sharepoint location. Create a shared folder in Sharepoint site where this batch job will save pdf files. I created SalesQuotations as a shared folder where files will be kept. 

Using Try and Catch block to handle any exceptions in batch job error log message.

public static void PublishQuotations(QuotationId _quotationId)

{

        try

        {

            SrsReportRunController                      controller = new SrsReportRunController();

            SalesQuotationContract                      contract = new SalesQuotationContract();

            SRSPrintDestinationSettings                 settings;

            Array                                       arrayFiles;

            System.Byte[]                               reportBytes = new System.Byte[0]();

            SRSProxy                                    srsProxy;

            SRSReportRunService                         srsReportRunService = new SrsReportRunService();

            CUSTQUOTATIONCONFIRMSALESLINK               custQuotationConfirmSalesLink;

            CustQuotationJour                           custQuotationJour;

            Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[]  parameterValueArray;

            Map                                         reportParametersMap;

            SRSReportExecutionInfo executionInfo = new SRSReportExecutionInfo();

            Args                                    args;

            args = new Args();

            controller.parmArgs(args);

            controller.parmReportName(ssrsReportStr(SalesQuotation, Report));

            controller.parmExecutionMode(SysOperationExecutionMode::Synchronous);

            controller.parmShowDialog(false);

            controller.parmLoadFromSysLastValue(false);

            select firstonly RecId from custQuotationJour

            where  custQuotationJour.QuotationDocNum == _quotationId;            

            contract.parmRecordId(custQuotationJour.RecId);

            contract.parmDocumentTitle("Sales Quotation");

            controller.parmReportContract().parmRdpContract(contract);

            settings = controller.parmReportContract().parmPrintSettings();     

            settings.printMediumType(SRSPrintMediumType::File);

            settings.fileFormat(SRSReportFileFormat::PDF);

            settings.overwriteFile(true);

            settings.fileName(_quotationId);

            controller.startOperation();                                         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());

            reportBytes = srsproxy.renderReportToByteArray(controller.parmreportcontract().parmreportpath(),

                                                      parameterValueArray,

                                                      settings.fileFormat(),

                                                      settings.deviceinfo());


            System.UriBuilder builder = new System.UriBuilder("https://xxxxx.sharepoint.com");

            str extId = xUserInfo::getCurrentUserExternalId();

            Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider provider;

            Microsoft.Dynamics.AX.Framework.FileManagement.DocumentLocation documentLocation = new Microsoft.Dynamics.AX.Framework.FileManagement.DocumentLocation();

            provider = new Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider("xxxxx.sharepoint.com", "sites/SalesQuotations","Shared%20Documents/SalesQuotations", extId);

            System.IO.MemoryStream mstream = new System.IO.MemoryStream(reportBytes);

            documentLocation = provider.SaveFile(newGuid(), _quotationId+ ".pdf",         System.Web.MimeMapping::GetMimeMapping(_quotationId + ".pdf"), mstream);

        }

        catch(Exception::Error)

        {

            error("File not saved successfully in shared location");

        }

}


No comments:

Post a Comment

Enable and Disable Vendor invoice button on PO list and details page X++

Hi folks, This blog will be useful where vendor invoice process is required to be controlled based on certain conditions. Requirements: Proc...