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 am

    I 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 4 years, 6 months ago 2 Members · 1 Reply
  • 1 Reply
  • Piyush

    Member
    September 3, 2019 at 7:55 am

    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[] {'[email protected]'} );
                System.debug('Send --'+ message);   
                lstEmail.add(message); 
            }         
            if(!lstEmail.isEmpty()){
                Messaging.sendEmail(lstEmail ); 
            }
        }
    }

     

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos