Hello Friends, In this post i will let you know  how you can send salesforce report as attachment through email using apex code. By using this method you can send reports in two way, first one is csv and second one is printable view in csv. Here i am describe both methods with sample code that helps you a lot. So le's begin,

Salesforce Report as attachment using Apex

To send salesforce report as attachment use one of the following method. In both methods the key point is URL.The url will decide which way you pick to send report through email. Let's see the code example.

Note: Both format and code sample only works in salesforce classic not in lightning experience. 

1. CSV Format

 List<Messaging.EmailFileAttachment> attachments=new List<Messaging.EmailFileAttachment>();
List<Report>reportList = [SELECT Id,DeveloperName,Name FROM Report where DeveloperName ='All_Account'];
if(reportList.size()>0){
for(Report report:reportList){
String reportId = (String)report.Id;
string reportName=(String)report.Name;
ApexPages.PageReference objPage = new ApexPages.PageReference('/'+reportId+'?csv=1&isdtp=p1');
Messaging.EmailFileAttachment objMsgEmailAttach = new Messaging.EmailFileAttachment();
objMsgEmailAttach.setFileName(reportName+'.csv');
if(!Test.isRunningTest())
objMsgEmailAttach.setBody(objPage.getContent());
objMsgEmailAttach.setContentType('text/csv');
attachments.add(objMsgEmailAttach);
}
}
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setUseSignature(false);
mail.setToAddresses(new List<String>{'smanisheng@gmail.com'});
mail.setSubject('Daily Report from Salesforce');
mail.setHtmlBody('<br/><br/>Please review daily reports attached.<br/><br/><br/><br/>');
mail.setFileAttachments(attachments);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

In where clause I am using developer name of report but you can use report id instead.

Output:


 

2. Printable View Format

 List<Messaging.EmailFileAttachment> attachments=new List<Messaging.EmailFileAttachment>();
List<Report>reportList = [SELECT Id,DeveloperName,Name FROM Report where DeveloperName ='All_Account'];
if(reportList.size()>0){
for(Report report:reportList){
String reportId = (String)report.Id;
string reportName=(String)report.Name;
ApexPages.PageReference objPage = new ApexPages.PageReference('/servlet/PrintableViewDownloadServlet?isdtp=p1&reportId='+reportId);
Messaging.EmailFileAttachment objMsgEmailAttach = new Messaging.EmailFileAttachment();
objMsgEmailAttach.setFileName(reportName+'.csv');
if(!Test.isRunningTest())
objMsgEmailAttach.setBody(objPage.getContent());
objMsgEmailAttach.setContentType('text/csv');
attachments.add(objMsgEmailAttach);
}
}
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setUseSignature(false);
mail.setToAddresses(new List<String>{'smanisheng@gmail.com'});
mail.setSubject('Daily Report from Salesforce');
mail.setHtmlBody('<br/><br/>Please review daily reports attached.<br/><br/><br/><br/>');
mail.setFileAttachments(attachments);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

After getting report email please download the report then open it, don't try to see report directly in email browser because of printable format it's not opening correctly in browser.

Output:


Hope you like this post, for any feedback or suggestions please feel free to comment. I would appreciate your feedback and suggestions.

Thank you.