Monday, February 27, 2023

Generate report using x++

 Generate report using x++.


By using the below code I am generating the report and adding it is adding as a attachment to that purchase order.


	Filename                        fileName = 'PurchaseOrder'.pdf';
	SrsReportRunController          controller = new SrsReportRunController();
	PurchaseOrderContract           contract = new PurchaseOrderContract();
	SRSPrintDestinationSettings     settings;
	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                             fileContentType;
	DocuRef                         docuRef;
	DocuValue                       docuValue;
	System.IO.MemoryStream          mstream;
	PurchTable                      purchTable;
	Filename                        attachmentName;

	select firstOnly purchTable
		where purchTable.PurchId == _purchId;

	delete_from docuValue 
		exists join docuRef
		where docuRef.RefRecId  == purchTable.RecId
		&& docuValue.RecId      == docuRef.ValueRecId;

	delete_from docuRef
		where docuRef.RefRecId  == purchTable.RecId;

	//set the contract parameters
	contract.parmPurchId(purchTable.PurchId);

	//set controller parameters
	controller.parmReportName('ReportName');//
	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);

	//set proxy and service values
	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());

	//conver the byte array to memory stream
	mstream         = new System.IO.MemoryStream(reportBytes);
	
        
        // To Download 
        Way 1:

        File::SendFileToUser(mstream, fileName);
        
        Way 2:
        attachmentName  = System.IO.Path::GetFileNameWithoutExtension(fileName);
	fileContentType = System.Web.MimeMapping::GetMimeMapping(fileName);
        System.IO.StreamReader sReader = new System.IO.StreamReader(mstream);
fileContent = sReader.ReadToEnd(); File::SendStringAsFileToUser(fileContent, fileName); //create file attachment in purchase order header with blob storage URL docuRef = DocumentManagement::attachFileToCommon(purchTable, DocuType::typeFile(),                                          mstream, fileName, fileContentType, attachmentName); //Modify the attachment's custom field details if(docuRef.RecId) { }





Keep Daxing!!

No comments:

Post a Comment