Update: Send SSRS Report as Email (with meaningful PDF File name)

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.

Import the ReportViewer.WinForms DLL

The DLL Microsoft.ReportViewer.WinForms is required. The DLL can be found at C:\Program Files (x86)\Microsoft Visual Studio 12.0\ReportViewer . Add the DLL as Reference in the AOT.

Parameter Table

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.

Report Mailing Parameter
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.
Report Mailing Parameter for SalesInvoice

find() and exist()

Add a find and exist method to the table.

public static ERPReportMailParameter find(SrsReportName _name,
SrsReportDesignName _design,
boolean _forupdate = false)
ERPReportMailParameter parm;
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;

Business Logic

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
parameterName = ERPReportMailParameter::find(_name,_design)

      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
case “SalesInvoice”:
documentName = ERPReportMailParameter::documentNameSalesInvoice

return documentName;

public static str documentNameSalesInvoice(str _parameterValue)
RecId recId = str2int64(_parameterValue);
return CustInvoiceJour::findRecId(recId).InvoiceId;

SrsReportViewer Form

At the SrsReportViewer Form, modify the init() method so the report parameters are always visible.

public void init()
boolean showReportViewerParameters;
        // showReportViewerParameters = this.controller()
        // set form help context.
//showReportViewerParameters = false;
    showReportViewerParameters = true;

Add a send button to the button group

Modify the clicked() method

void clicked()
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();


   axviewer = AxReportViewer.control();
nviewer = axviewer.get_ReportViewerControl();

report = nviewer.get_ServerReport();
reportPath = report.get_ReportPath();


      // cut the report server path
slash = strFind(reportPath,”/”,strLen(reportPath),
reportName = subStr(reportPath,slash+1,strLen(reportPath)-slash);

      // split name and design
dot = strFind(reportName,”.”,strLen(reportName),
srsReportName = subStr(reportName,1,dot-1);
srsReportDesignName = subStr(reportName,dot+1,
documentName = ERPReportMailParameter::documentName

      //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);

      //open outlook email
if (smmOutlookEmail.createMailItem())
ex = CLRInterop::getLastException();

Test it

Compile your code in X++ and IL and test it.

Send SSRS Report per Email

Split long text in SSRS reports on two pages

By default text in tables on Dynamics AX reports like SalesQuotation are kept together. If the text does not fit on the actual page, the complete line starts on the next page. This wastes lot of space and produces unnecessary many pages.


To split text in the SalesQuotation follow these steps:

  1. Start Visual Studio as Admin and load the SalesQuotation report from the Application Explorer
  2. Open the report design
  3. Add a new line under the line that contains [ItemId], [Name], [DlvDate] etc.image
  4. Assign the same fields in the new line, but don’t assign the [Name] field
  5. Select the new line and set the property KeepTogether to False
  6. Drag&Drop a textfield form the toolbox on an empty space in the report and name it textboxName
  7. Drag&Drop the textfield in the empty column in the new line.
  8. Make sure the textfield “textboxName” is still selected and open the properties window. Set the field [Name] as value for the textfield
  9. Change the Font and Size to fit the other fields, by default its Segoe UI 8pt
  10. Finally mark the old line above and set the property Hidden to True
  11. Deploy the report