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
Michelle SotoMichelle Soto 

How can I send an email with a salesforce report attached?

I'm try to scheduler email send with a repor attached, but when i received the email, appear the next error in the file:

User-added image
global class Exporter implements System.Schedulable {
    global void execute(SchedulableContext sc) {
        DoExport();
    }

    @future(callout=true)
    public static void DoExport() {
        ApexPages.PageReference report = new ApexPages.PageReference('/00O6g000004Z0nNEAS?csv=1');
        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
        attachment.setFileName('report.csv');
        attachment.setBody(Blob.valueof(report.getContent().toString()));
        attachment.setContentType('text/csv');
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } );
        message.setPlainTextBody('The report is attached.');
        message.setToAddresses( new String[] { 'user@example.com' } );
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );
    }
}
Could you help me with this error please?
Could you tell me if there is another option to solve the need to send the attached file by mail?
 
Best Answer chosen by Michelle Soto
SwethaSwetha (Salesforce Developers) 
HI Michelle,
Does the approach suggested in https://salesforcescool.blogspot.com/2021/06/send-salesforce-report-as-attachment.html work for you?

Also see similar scenario: https://salesforce.stackexchange.com/questions/283865/convert-report-data-to-csv-format-and-send-attachment-in-mail-using-apex
 Thanks

All Answers

SwethaSwetha (Salesforce Developers) 
HI Michelle,
Does the approach suggested in https://salesforcescool.blogspot.com/2021/06/send-salesforce-report-as-attachment.html work for you?

Also see similar scenario: https://salesforce.stackexchange.com/questions/283865/convert-report-data-to-csv-format-and-send-attachment-in-mail-using-apex
 Thanks
This was selected as the best answer
mukesh guptamukesh gupta
Hi Michelle,

Below url will help to solve your problem:-

https://salesforcecodex.com/salesforce/sending-report-as-attachment-in-salesforce/

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh