Recently my team got requirement to send report as email attachment to external users. This blog will show number of ways we can send report as attachment in email.
We can send report to our internal user using standard schedule report but we can not schedule to send email to external user.
Here are some options which can be used to solve this requirement.
- Create attachment using SOQL and add data as attachment
- Create attachment using Apex from standard report
- Create user and send to that user email
Create attachment using SOQL and add data as attachment
We can create CSV from data which is queried using SOQL. Then we can send that generated CSV file as attachment. Let us take example we need to share all account detail which are opened using our custom API . Here is code which will be used to share email with above mentioned data.
Steps:
1. Create SOQL
Create SOQL to get data from object. I have fetched 10 records from account. These will come as CSV records.
List<Account> acconts = [SELECT Id,Name,BillingState FROM Account LIMIT 10];
2. Convert data into CSV
Put all record data as string.
string header = 'Id, Account Name, BillingState\n'; string finalstr = header; if(acconts !=null && acconts.size()>0){ for(Account act: acconts) { string recordString = act.Id+','+act.Name+','+act.BillingState+'\n'; finalstr = finalstr +recordString; } }
3. Convert string data in CSV
Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment(); blob csvBlob = Blob.valueOf(attachFiles.get(name)); string csvname=name+'.csv'; csvAttc.setContentType('text/csv'); csvAttc.setFileName(csvname); csvAttc.setBody(csvBlob);
4. Send Email using EmailService class
Create a generic EmailService class. This class will send email to given user.
Complete Code:
Create attachment using Apex from standard report
We can send standard report as attachment as well. For this first create standard report and get report Id to be used in apex code.
Steps
1. Create SOQL
Get report detail using SOQL from Report object.
List <Report> reportList = [SELECT Id,DeveloperName,Name FROM Report where DeveloperName =:reportDevName]; String reportId = (String)reportList.get(0).get('Id'); //Get Report Name string reportName=(String)reportList.get(0).get('Name');
2. Get report content
Get report content using java servlet for specific report. Add report id from above mentioned step.
String instanceName = URL.getSalesforceBaseUrl().toExternalForm(); string url=instanceName+'/servlet/PrintableViewDownloadServlet?isdtp=p1&reportId='+reportId; ApexPages.PageReference objPage = new ApexPages.PageReference(url); Blob content=objPage.getContent(); //Get report content
3. Convert content into CSV
Convert blob into CSV file and add content into EmailFileAttachement.
Messaging.EmailFileAttachment objMsgEmailAttach = new Messaging.EmailFileAttachment(); objMsgEmailAttach.setFileName(reportName+'.csv'); objMsgEmailAttach.setBody(objPage.getContent()); objMsgEmailAttach.setContentType('text/csv');
Code:
Use EmailService class which is shown in first option’s code file.
Create user for external users and send to that user’s email
Refer solution outlined at Send a Scheduled Report Email to multiple addresses outside of Salesforce for this option.
7 comments
This is very helpful. Thank you.
Thank You @Sirisha
Great! Worked. Thank you!
Thank You @Prabakaran
Can someone explain how I can test this code out. ?
I created the two classes EmailService and ReportGenerationController .
What do I need to do to see the output as shown in Sample Report?
Hello Jill,
You have to create standard report and you have to pass report developer name (API name) in ReportController.generateReport method. That will work.
Thank You,
Dhanik