Here I am generating the report through code and sending that report to cusomer using X++.
class ConfirmSalesOrdersService extends SysOperationServiceBase { public void cnfOrdersAndSendMail() { SalesTable salesTable; SalesFormLetter salesFormLetter; CustConfirmJour custConfirmJour; while select salesTable where salesTable.DocumentStatus == DocumentStatus::None && salesTable.SalesType == SalesType::Sales && salesTable.AutoOrderCnfirm == NoYes::Yes { if(salesTable.RecId) { try { salesFormLetter = SalesFormLetter::construct(DocumentStatus::Confirmation); salesFormLetter.update(salesTable, DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone()), SalesUpdate::All); select firstonly custConfirmJour where custConfirmJour.SalesId == salesTable.SalesId; this.getReport(custConfirmJour.RecId,salesTable.customerEmail(),salesTable.SalesId); } catch { //Error("%1",infolog.text()); this.getErrorStr(); continue; } } } } public void getReport(RecId recId,Email custEmail,SalesId salesId) { Filename fileName = strFmt("%1_%2",salesId,"ConfirmationJournal.pdf"); SrsReportRunController controller = new SrsReportRunController(); SalesConfirmContract contract = new SalesConfirmContract(); 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(); contract.parmRecordId(recId); // Provide details to controller and add contract controller.parmArgs(new Args()); controller.parmReportName(ssrsReportStr(SalesConfirm, Report)); 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()); // 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); // download file //System.IO.StreamReader sReader = new System.IO.StreamReader(stream); //fileContent = sReader.ReadToEnd(); //File::SendStringAsFileToUser(fileContent, fileName); // System.IO.File::WriteAllBytes("C:\\backup\\"+fileName,reportBytes); // You can save the file on your local instance for verification if(binaryData) { binData = binaryData.getContainer(); } System.Byte[] binData1; System.IO.Stream stream1; // Turn the Bytes into a stream for(int i = 0; i < conLen(binData); i++) { binData1 = conPeek(binData,i+1); stream1 = new System.IO.MemoryStream(binData1); } this.sendEmail(custEmail,"Order Confirmation",stream1,salesId,fileName);//custEmail } public void sendEmail(Description _emailTo, Description _Subject,System.IO.MemoryStream stream1,SalesId _salesId,FileName _filename) { SysMailerSMTP mailer = new SysMailerSMTP(); SysMailerMessageBuilder builder = new SysMailerMessageBuilder(); SysEmailParameters parameters = SysEmailParameters::find(); Filename fileName = _filename; //you will have to setup the below parameters in sysadmin module if (parameters.SMTPRelayServerName) { mailer.SMTPRelayServer(parameters.SMTPRelayServerName, parameters.SMTPPortNumber, parameters.SMTPUserName, SysEmailParameters::password(), parameters.SMTPUseNTLM); } else { warning("SERVER NOT FOUND"); } builder.setFrom(SysEmailParameters::find().SMTPUserName); builder.addTo(_emailTo); builder.setSubject(_Subject); builder.setBody(strFmt("%1,%2","Order id:",_salesId)); if(stream1 != null) { builder.addAttachment(stream1, fileName); } SysMailerFactory::getNonInteractiveMailer().sendNonInteractive(builder.getMessage()); } private str getErrorStr() { SysInfologEnumerator enumerator; SysInfologMessageStruct msgStruct; Exception exception; str error; ; enumerator = SysInfologEnumerator::newData(infolog.cut()); while (enumerator.moveNext()) { msgStruct = new SysInfologMessageStruct(enumerator.currentMessage()); exception = enumerator.currentException(); error = strfmt("%1 - %2", error, msgStruct.message()); } return error; } }
Keep Daxing!!
No comments:
Post a Comment