ABSYZ ABSYZ

  • Home

    Welcome to ABSYZ

  • About us

    Who We Are

  • Our Expertise

    What We Do

  • Our Approach

    How We Do It

  • Products

    What We Made

  • Industries

    Who We Do It For

  • Clients

    Whom We Did It For.

  • Article & Blogs

    What Experts Think

  • Careers

    Join The Team

  • Get In Touch

    Let’s Get Started

ABSYZ

SENDING REPORTS TO EMAIL AS AN ATTACHMENT

Home / Article & Blogs / Apex / SENDING REPORTS TO EMAIL AS AN ATTACHMENT

SENDING REPORTS TO EMAIL AS AN ATTACHMENT

By Katigari Pradeep Kumar Reddy inApex, Email Service

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}”/>
<apex:outputPanel>
<table style=”width: 100%;”>
<thead>
<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>
</thead>
<tbody>
<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:repeat>
</tbody>
</table>
</apex:outputPanel>
</apex:component>
[/sourcecode]

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;
}
}
[/sourcecode]

3. Create a Visualforce Email Template

EmailTemplate

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:plainTextEmailBody>
<messaging:attachment filename=”Account Report.xls”>
<!– Here we can add multiple Reports –>
<c:ReportsToEmail ReportId=”xxxxxxxxxxxxxxxxxxxx”/>
<c:ReportsToEmail ReportId=”xxxxxxxxxxxxxxxxxxxx”/>
</messaging:attachment>
</messaging:emailTemplate>
[/sourcecode]

“ <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.

EmailReportsvisualforce componentVisualforce Email Template
48
Unlike this post
1 Post
Katigari Pradeep Kumar Reddy

Search Posts

Archives

Categories

Recent posts

Service Cloud for customer delight

Service Cloud for customer delight

Salesforce Financial Services Cloud

Salesforce Financial Services Cloud

Salesforce Field Service Lightning

Salesforce Field Service Lightning

Connecting the dots with Customer 360

Connecting the dots with Customer 360

Customer delight with Commerce Cloud

Customer delight with Commerce Cloud

  • Previous PostThank you for helping us become Platinum
  • Next PostSingle-Sign-On and SAML in Salesforce

Related Posts

REST API call from Einstein Analytics Dashboard
Apex REST Salesforce Salesforce Einstein Wave Analytics

REST API call from Einstein Analytics Dashboard

5 Comments

  1. Manjunath
    Reply
    6 March 2020

    Hi bro, this is an wonderful blog, i have similar task where i have created a vf component and apex class and i have created VF template, but the issue is when i try i am getting an report but the alignment is not proper, how did you solve this issue

    Reply
    • Kathigari Pradeep Kumar Reddy
      Reply
      6 March 2020

      Hi Manjunath,
      If you need to get the proper alignment then you need to use CSS in vf component. Here I am using the HTML table to display content in the attachment.

      Reply
  2. Manjunath
    Reply
    9 March 2020

    Hey bro, i tried designing Css for Vf component, still i am getting the same error, if you dont mind can you send me snapshot of the css design so that i can do the modifications according to my requirement, here i cannot post the error snapshot, please send me your email id

    Reply
    • Katigari Pradeep Kumar Reddy
      Reply
      10 March 2020

      Hi Manjunath,

      Can you please share the error snapshot to this email id pradeep.katigari@gmail.com

      Reply
  3. Mnjunath
    Reply
    12 March 2020

    Hello Pradeep

    It is working now, thank you very much for the blog, this was very informative

    Reply

Leave a Reply (Cancel reply)

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

*
*

ABSYZ Logo
  • Home
  • About us
  • Article & Blogs
  • Careers
  • Get In Touch
  • Our Expertise
  • Our Approach
  • Products
  • Industries
  • Clients
  • White Papers

ABSYZ Software Consulting Pvt. Ltd.
USA: 49197 Wixom Tech Dr, Wixom, MI 48393, USA
M: +1.415.364.8055

India: 6th Floor, SS Techpark, PSR Prime, DLF Cyber City, Gachibowli, Hyderabad, Telangana – 500032
M: +91 79979 66174

Copyright ©2020 Absyz Inc. All Rights Reserved.

youngsoft
Copy