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.
6 Comments
Is it possible to add filter values to the report?
ReplyDeleteyes, it's possible
DeleteCan you give an example on hot to do it with a dynamic filter? I have tried the url below and is not applying the filter
ReplyDeleteApexPages.PageReference objPage = new ApexPages.PageReference('/'+reportId+'?fv1='+ filterValue + '&csv=1&isdtp=p1');
Use following url,
DeleteApexPages.PageReference objPage = new ApexPages.PageReference('/'+reportId+'?pv0='+ filterValue + '&csv=1&isdtp=p1');
Thank you! that did the trick!
DeleteIs it possible to also filter it in the Printable View Format? I wasn't able to make it work with the 'pv0='+filterValue
The original line for the PageReference is "ApexPages.PageReference objPage = new ApexPages.PageReference('/servlet/PrintableViewDownloadServlet?isdtp=p1&reportId='+reportId);
ReplyDeleteHow to you propose to add the filter value with your suggestion:
ApexPages.PageReference objPage = new ApexPages.PageReference('/'+reportId+'?pv0='+ filterValue + '&csv=1&isdtp=p1');
That does not appear to work. Is there any way to add a filter value that incorporates the URL hack for "'/servlet/PrintableViewDownloadServlet?"
Thanks.
Post a Comment