Activity › Forums › Salesforce® Discussions › How to send Reports via email to a group of users on an hourly basis
-
How to send Reports via email to a group of users on an hourly basis
Posted by Yogesh on August 30, 2019 at 8:12 AMI want to know that whether it is possible to send reports data as a content via email to a group of users, on an hourly basis .
Piyush replied 6 years, 9 months ago 2 Members · 1 Reply -
1 Reply
-
Hi Yogesh,
You can take help from this example:-
global with sharing class ScheduleandEmailReport implements schedulable { global void execute(SchedulableContext SC){ // Get the report ID scheduleJob(); } @future(callout=true) private static void scheduleJob(){ List <Report> reportList = [SELECT Id,DeveloperName FROM Report where DeveloperName = 'Account_Report_for_Email' ]; System.debug(reportList); List<Messaging.SingleEmailMessage> lstEmail = new List<Messaging.SingleEmailMessage>(); Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); List< Messaging.EmailFileAttachment> lstAttachment = new List< Messaging.EmailFileAttachment>(); String[] reportId = new String[reportList.size()]; String[] reportname = new String[reportList.size()]; for(Integer i=0;i < reportList.size();i++) { //Getting record on the basis of Report Id and puttinf it into Excel as attachment. reportId[i] = (String)reportList.get(i).get('Id'); reportname[i] = (String)reportList.get(i).get('DeveloperName'); ApexPages.PageReference report = new ApexPages.PageReference( '/' + reportId[i] + '?excel=1'); // or csv=1 Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment(); System.debug('---report--'+ report); attachment.setFileName(reportname[i] + '.xls'); Blob content = !Test.isRunningTest() ? report.getContent() : Blob.valueOf('Test'); attachment.setBody(content); attachment.setContentType('application/vnd.ms-excel'); // or text/csv lstAttachment.add(attachment); message.setFileAttachments(lstAttachment); message.setSubject('Report: ' + 'All Extracted Reports'); message.setHtmlBody(content.toString()); message.setToAddresses( new String[] {'abcd@xyz.com'} ); System.debug('Send --'+ message); lstEmail.add(message); } if(!lstEmail.isEmpty()){ Messaging.sendEmail(lstEmail ); } } }
Log In to reply.