Emailing reports using a single analysis
This example uses an analysis containing sales data for an organization, with different data for multiple users and groups. Based on the data, PDF reports will be created for various sales regions and will be sent to the different sales managers.
The job contains the tasks:
Open Analysis from Library
(/Distributions/Sales Figures Template)
ForEach
(columns: FilterValue;e-mail)
Execute Script
(uses row value from
{forEachProperty:FilterValue})
Export Report to PDF
(Report SalesReport to C:\Temp\PdfReport_{jobid}.PDF)
Send Email
(Sends email to {forEachProperty:e-mail}, attach
C:\Temp\PdfReport_{jobid}.PDF)
ForEach End
![](../images/example_tasks.png)
Open Analysis from Library
![](../images/open_analysis_from_library.png)
ForEach
The ForEach task holds the table to iterate over. The table consists
of two columns: one for the filter value used to specify the sales manager's
region,
FilterValue
, and another for the sales manager's email
address,
e-mail
. The embedded table looks like this:
FilterValue;e-mail
CENTRAL;sales.east@domain.com
EAST;sales.east@domain.com
WEST;sales.west@domain.com
![](../images/foreach_task.png)
ForEach End indicates the end of the ForEach loop.
Execute Script
The IronPython script uses theCheckBox
filter
Region
to show only the data related to a specific sales
region in the analysis. It fetches different region values using
functionality
({forEachProperty:FilterValue})
. The scripts have a
predefined object called
AutomationContext
, which helps access the entire
Spotfire API using
AutomationContext.Application
.
from Spotfire.Dxp.Application.Filters import CheckBoxFilter
# Constants for the name of the data table, and filter
dataTableName = "SuperStore Sales"
columnName = "Region"
# Get the filter value to set from the ForEach table.
filterValueToSet = "{forEachProperty:FilterValue}"
# Get main objects from execution context
application = AutomationContext.Application
document = application.Document
# Get the filter to filter the regions
dataTable = document.Data.Tables[dataTableName]
column = dataTable.Columns.Item[columnName]
filter=document.FilteringSchemes[0].Item[dataTable].Item[column].As[CheckBoxFilter]()
if filter is None:
raise LookupError("Checkbox filter not found for column '{0}' in data table '{1}' in analysis '{2}'.".format(columnName, dataTableName, application.DocumentMetadata.LoadedFromLibraryPath))
# The filter "Check" will not raise an error if it is not found, so make sure the value exists
if not filter.Values.Contains(filterValueToSet):
raise LookupError("Filter value '{0}' not found in column '{1}' in analysis '{2}'.".format(filterValueToSet, columnName, application.DocumentMetadata.LoadedFromLibraryPath))
# Check only the region from the current row in the ForEach table
filter.UncheckAll()
filter.Check(filterValueToSet)
![](../images/execute_script.png)
Export Report to PDF
This task exports the predefined PDF report,
SalesReport, to a temporary PDF file located at
C:\Temp\PdfReport_{jobid}.PDF. The task uses the
field
{jobid}
to make sure the filename is unique.
![](../images/export_report_to_pdf.png)
Send Email
This task retrieves the exported PDF report using Attached files at the bottom. To add the file, select Add and copy the path from the Export Report to PDF task: C:\Temp\PdfReport_{jobid}.PDF. Check the box Delete the attachment after sending the mail to remove the temporary file. In the field, To, use to insert the email value{forEachProperty:Email}
.![](../images/send_email_settings.png)