Sending reports to email as an attachment

Sending reports to email as an attachment

We all know how to create reports and dashboards, but the challenge arises when we need to send reports to email as an excel attachment. Salesforce doesn’t provide us with standard inbuilt functionality to do so. I too faced the same challenge and built a solution for this. We have to follow certain steps to achieve this approach.

  1. Create a report
  2. Create a Visualforce Component
  3. Create a Visualforce Email Template

1. Create a report

Creating a report or we can take any existing report which is already present in the Org. And make sure that the report preview format is the tabular format.

2. Create a Visualforce Component

Next, we need to create a Visualforce component to get the report data and to display as a report. Here we are getting the report Id from the Email Template.

Visualforce Component: ReportsToEmail

[sourcecode language=”java”]
<apex:component controller=”ReportsToEmailController” access=”global”>
<apex:attribute name=”ReportId” description=”Report ID” type=”Id” assignTo=”{!rptId}”/>
<table style=”width: 100%;”>
<apex:repeat value=”{!ReportResult.reportMetadata.detailColumns}” var=”colName”>

<!– reportMetadata is a class where it contains metadata of a report.
DetailColumns is a method of ReportMetadata class, it returns the API names (Columns Names)
for the fields that contain detailed data–>

<th><apex:outputText value=”{!ReportResult.reportExtendedMetadata.detailColumnInfo[colName].label}”/></th>

<!– reportExtendedMetadata is class where it contains Report extended metadata and
it provides data type and label information.
detailColumnInfo is a method of reportExtendedMetadata class, it returns map of columns names
and its label to Display as Header –>

<apex:repeat value=”{!ReportResult.factMap[‘T!T’].rows}” var=”row” rows=”999″>
<!– Here we will get entire data of each row and T refers to the Row –>
<tr> <apex:repeat value=”{!row.dataCells}” var=”cell”>
<!– Here we will get data of each cell and displayed –>
<td><apex:outputText value=”{!cell.label}”/></td>
</apex:repeat> </tr>


Apex Controller: ReportsToEmailController

[sourcecode language=”java”]
public class ReportsToEmailController {
public Id rptId { get; set; } // Here we will get the report Id from the VF Component
private transient Reports.ReportResults results; // It will hold the entire data of a report

// Method Name : getReportResult
// Description : Here we will get the data of a report and send to the VF Component

public Reports.ReportResults getReportResult() {
// Here it will run the report with that report ID and adding the report data into results
results = Reports.ReportManager.runReport(rptId, true);
return results;

3. Create a Visualforce Email Template


After creating that we need to edit the template and add the visualforce component as an attachment.

[sourcecode language=”java”]
<messaging:emailTemplate subject=”Report” recipientType=”User” >
<messaging:plainTextEmailBody >
Hi {!recipient.FirstName}
Please find the below attachments.

<messaging:attachment filename=”Account Report.xls”>
<!– Here we can add multiple Reports –>
<c:ReportsToEmail ReportId=”xxxxxxxxxxxxxxxxxxxx”/>
<c:ReportsToEmail ReportId=”xxxxxxxxxxxxxxxxxxxx”/>

 <c:ReportsToEmail ReportId=”xxxxxxxxxxxxxxxxxx”/> ” Here I have called the VF component and we need to give the Report ID. In filename I am giving the name of the file and the type of the attachment(.xls), it automatically attaches the file to an email.

This Email Template can be used in Email Alerts or in apex class and from there you can send the reports to Email.

Here I am using a checkbox field to send email, using workflow whenever this checkbox field is true I am sending the reports to the email using email alert and then I am unchecking the checkbox using field update.

Leave a Comment

Your email address will not be published. Required fields are marked *

Recent Posts

how can salesforce experience cloud drive customer engagement rates
How Can Salesforce Experience Cloud Drive Customer Engagement Rates?
salesforce world tour essentials mumbai
Salesforce World Tour Essentials Mumbai (19 June, 2024)
benefits of using salesforce for higher education Industry
Benefits Of Using Salesforce For Higher Education Industry?
salesforce for real estate
Salesforce for Real Estate: How Salesforce CRM Can Help Realtors
salesforce consumer goods cloud store setup
A Detailed Look into Salesforce Consumer Goods Cloud Store Setup
Scroll to Top