function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Manju053Manju053 

Email Report with csv attachment

Hello Developers

My Requirement is that Reports must be sent through email with csv attachments, i have refered several blogs,

Basically Report needs to go in email with csv format, i have designed a apex class, but i dont know how to excute it in ananomus window
 
global class sendreport implements System.Schedulable {
    global void execute(SchedulableContext sc) {
        ApexPages.PageReference report = new ApexPages.PageReference('00O2w000002RDbUEAW'); //this id of the report
        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
        attachment.setFileName('report.xls');
        attachment.setBody(report.getContent());
        attachment.setContentType('text/csv');
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } );
        message.setSubject('Report');
        message.setPlainTextBody('The report is attached.');
        message.setToAddresses( new String[] { 'manjunath.s@proseraa.com' } );
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );
        
    }
 }
Any help please?

 
VinayVinay (Salesforce Developers) 
Hi Denzel,

Please find below details.

 Go to Setup --> Open Developer Console.
 Select "Debug" tab --> Open Execute Anonymous Window
 In this window, type
Database.executeBatch(new MyClass());

Please make sure to replace MyClass with the apex batch class name that you have written/ want to execute.

Also review below article on how to execute batch Apex from Developer Console

https://help.salesforce.com/articleView?id=000328480&language=en_US&type=1&mode=1

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Thanks,
Vinay Kumar
Manju053Manju053
@Vinay

Thanks, My Original Question was report should be sent from salesforce with report attachments in csv format, i tried from the above code
but its not working, can you guide me please or give any suggestion
 
global class SendReportWithAttachement {
    global static void finish()
    {      
        blob attachbody;
        Report report1 = [SELECT Id, Name, DeveloperName FROM Report where 
                          DeveloperName = 'Opportunity_Dummy_Report_rKn' ];
        
        
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        List< Messaging.EmailFileAttachment> lstAttachment = new List< Messaging.EmailFileAttachment>();
        
        
        ApexPages.PageReference report22 = new ApexPages.PageReference( '/' + report1.id + '?excel=1'); 
       
        ApexPages.PageReference report = new ApexPages.PageReference('/00O4P000005MlOMUA0?excel=1');
        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
        attachment.setFileName('report.xlsx');
        attachment.setBody(report.getContent());
        attachment.setContentType('application/vnd.ms-excel');
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } );
        message.setSubject('Report');
        message.setPlainTextBody('The report is attached.');
        message.setToAddresses( new String[] { 'manjuzmail053@gmail.com' } );
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );
    }
}


I tried from the several links but stil it did not get resolved, 

Link - https://www.forcetalks.com/salesforce-topic/how-to-send-reports-via-email-to-a-group-of-users-on-an-hourly-basis/

Link - https://salesforce.stackexchange.com/questions/283865/convert-report-data-to-csv-format-and-send-attachment-in-mail-using-apex

i am getting an email with attach but i am not able to see the data inside the atachment

This is what i am getting

User-added image



This is what i am expecting

User-added image
Pedro Garcia 26Pedro Garcia 26
Hi... I'm facing the same problem. Did you solve it? Thanks