- The DLL Microsoft.ReportViewer.WinForms is required. The DLL can be found at C:Program Files (x86)Microsoft Visual Studio 12.0ReportViewer . Add the DLL as Reference in the AOT.
- Create a table that contains 3 fields and stores the combination of Report Name, Report Design and Report Parameter. Create an index for the combination of Report Name and Report Design.
A sample configuration for SalesInvoice looks like this: The Report Name is SalesInvoice, the Report Design is Report and the Parameter used to generate the document name is RecordId.

- Add a find and exist method to the table
public static ERPReportMailParameter find(SrsReportName _name, SrsReportDesignName _design, boolean _forupdate = false) { ERPReportMailParameter parm; parm.selectForUpdate(_forupdate); select firstOnly parm where parm.ReportName == _name && parm.ReportDesignName == _design; return parm; }
public static boolean exist(SrsReportName _name, SrsReportDesignName _design) { return ERPReportMailParameter::find(_name,_design).RecId > 0; }
- Create the following documentName* methods to generate the name of the PDF file. These methods are called by the SrsReportViewer Form to get a meaningful name for the PDF file in the mail.
public static str documentName(SRSReportName _name, SRSReportDesignName _design, Microsoft.Reporting.WinForms.LocalReport _report) { Microsoft.Reporting.WinForms.ReportParameterInfoCollection parms; Microsoft.Reporting.WinForms.ReportParameterInfo reportParm; System.Collections.IList list; System.Object value; str documentName = "Report"; // default value str parameterName = ""; str parameterValue = ""; // Is a parameter configured if(ERPReportMailParameter::exist(_name,_design)) { parameterName = ERPReportMailParameter::find(_name,_design) .ReportParameter; parms = _report.GetParameters(); reportParm = parms.get_Item(parameterName); list = reportParm.get_Values(); value = list.get_Item(0); parameterValue = value.ToString(); // Call document specific logic to determine the document name switch(_name) { case "SalesInvoice": documentName = ERPReportMailParameter::documentNameSalesInvoice (parameterValue); break; default: break; } } return documentName; }
-
public static str documentNameSalesInvoice(str _parameterValue) { RecId recId = str2int64(_parameterValue); return CustInvoiceJour::findRecId(recId).InvoiceId; }
- At the SrsReportViewer Form, modify the init() method so the report parameters are always visible.
public void init() { boolean showReportViewerParameters; super(); if(this.controller()) { // showReportViewerParameters = this.controller() .parmShowReportViewerParameters(); this.setFormCaption(); // set form help context. this.setFormHelpContext(); } else { //showReportViewerParameters = false; } showReportViewerParameters = true;
- Add a Send button to the Button Group

- Modify the clicked Method
void clicked() { Microsoft.Dynamics.AX.Frameworks.Controls.ReportViewer.AxReportViewer axviewer; Microsoft.Reporting.WinForms.ReportViewer nviewer; Microsoft.Reporting.WinForms.LocalReport report; ERPReportMailParameter mailParameter; str parameterName; str reportName; str reportPath; int dot; int slash; str documentName = "Report"; SRSReportName srsReportName; SRSReportDesignName srsReportDesignName; System.Exception ex; System.Array bytear; System.Object no; System.IO.File file; str tmpPath; System.IO.FileStream fs; SmmOutlookEmail smmOutlookEmail = new SmmOutlookEmail(); super(); axviewer = AxReportViewer.control(); nviewer = axviewer.get_ReportViewerControl(); try { report = nviewer.get_ServerReport(); reportPath = report.get_ReportPath(); // cut the report server path slash = strFind(reportPath,"/",strLen(reportPath), strLen(reportPath)*-1); reportName = subStr(reportPath,slash+1,strLen(reportPath)-slash); // split name and design dot = strFind(reportName,".",strLen(reportName), strLen(reportName)*-1); srsReportName = subStr(reportName,1,dot-1); srsReportDesignName = subStr(reportName,dot+1, strLen(reportName)-dot); documentName = ERPReportMailParameter::documentName (srsReportName,srsReportDesignName,report); //render as PDF bytear = report.Render("PDF"); no = bytear; //path to temp. files tmpPath = System.IO.Path::GetTempPath(); tmpPath = tmpPath + documentName + ".pdf"; //save to file fs = System.IO.File::Create(tmpPath); fs.Write(no,0,bytear.get_Length()); fs.Flush(); fs.Close(); //open outlook email if (smmOutlookEmail.createMailItem()) { smmOutlookEmail.isHTML(true); smmOutlookEmail.addFileAsAttachment(tmpPath); smmOutlookEmail.sendEMail(smmSaveCopyofEmail::No); } } catch(Exception::CLRError) { ex = CLRInterop::getLastException(); info(ex.ToString()); } }
- Compile in X++ and IL and test it
Update: Send SSRS Report as Email (with meaningful PDF File name)
与本文相关的文章
- 更新后 Dynamics 365 财务和运营字段丢失 / Dynamics 365 Finance and Operations field missing after update
- D365商业对 Adyen支付和服务连接器的考虑进行修改 / D365 Commerce making changes to Adyen payment and service connector considerations
- Solved: D365 Fields automatically populating on form errantly – Due to a Chrome addin/已解决:D365 字段自动错误地填充到表单上——由 Chrome 插件引发
- D365全屏模式,适用于现代POS(MPOS)/ D365 Full screen mode for Modern POS (MPOS)
- Dynamics 365 / AX 2012 现代/企业版POS’速度扫描’功能 / Dynamics 365/AX 2012 Modern/Enterprise POS ‘Speed Scanning’ feature
- Microsoft 365、Dynamics 365、Office 365 品牌和术语 / Microsoft 365 vs Dynamics 365 vs Office 365 terminology and branding (hopefully) explained