A while ago I’ve described how to add a “Send E-Mail Button” to the Report Viewer in Dynamics AX 2012: https://erpcoder.wordpress.com/2013/01/07/send-ssrs-report-as-email/. In this initial script all reports were generated as Report.PDF file. However, a visitor recently asked how to generate a PDF file, named according to the documents ID, e.g. To create an Invoice PDF named as the Invoice Number in AX.
This can be achieved by reading the SSRS Report Parameters and create a meaningful document name based on this information. For example, the SalesInvoice report holds a parameter RecordId, which is the RecId for the corresponding CustInvoiceJour record in Dynamics Ax.
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.
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;
}