Skip to content

Instantly share code, notes, and snippets.

@dhaniksahni
Created April 8, 2020 12:29
Show Gist options
  • Save dhaniksahni/ebe613b78dc69bc9cf8b6ea221400a2d to your computer and use it in GitHub Desktop.
Save dhaniksahni/ebe613b78dc69bc9cf8b6ea221400a2d to your computer and use it in GitHub Desktop.
Report Controller with Report
public class ReportController{
//Generate report Based on parameter
public void generateReport(string reportDevName)
{
try
{
List <Report> reportList = [SELECT Id,DeveloperName,Name FROM Report where DeveloperName =:reportDevName];
if(reportList.size()>0)
{
String reportId = (String)reportList.get(0).get('Id');
//Get Report Name
string reportName=(String)reportList.get(0).get('Name');
//get instance Url
String instanceName = URL.getSalesforceBaseUrl().toExternalForm();
string url=instanceName+'/servlet/PrintableViewDownloadServlet?isdtp=p1&reportId='+reportId;
ApexPages.PageReference objPage = new ApexPages.PageReference(url);
Messaging.SingleEmailMessage email=new Messaging.SingleEmailMessage();
Messaging.EmailFileAttachment objMsgEmailAttach = new Messaging.EmailFileAttachment();
objMsgEmailAttach.setFileName(reportName+'.csv');
objMsgEmailAttach.setBody(objPage.getContent());
objMsgEmailAttach.setContentType('text/csv');
email.setSubject(reportName);
List<Messaging.EmailFileAttachment> attach=new List<Messaging.EmailFileAttachment>();
attach.add(objMsgEmailAttach);
email.setFileAttachments(attach);
EmailService service=new EmailService(email);
service.body='Hello, <br/><br/> Here is attached '+reportName+'.<br/><br/>Thank You.<br/>Admin';
service.isHtml=true;
service.toAddresses=new List<string>();
service.toAddresses.add('contactus@salesforcecodex.com');
service.toAddresses.add('dhanik.sahni@yahoo.com');
service.displayName='Salesforce Codex';
service.subject=reportName;
service.sendMail();
}
}
catch(Exception ex)
{
system.debug(ex.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment