1

if I run this code in developer console, I receive the attached csv in an email. However the output of the code is javsscript code instead of the data

enter image description here

    String ReportId = '00O3300000331x3XXX'; 
    ApexPages.PageReference report = new ApexPages.PageReference('/'+ReportId+'?csv=1');
    Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();        
    attachment.setFileName('report.csv');
    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[] { '[email protected]' } );

    Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );

1 Answer 1

1

For some reason, getting a report's content is considered a callout. If you schedule this class, it fails and sends this bad file.

Create another method with @future(callout=true) and call it from the scheduled class. It should work then.

Also, unless you have checking logic elsewhere, this email will likely be sent even when the report has no rows.

Source: had this same issue when emailing an excel version of a report via apex.

Edit: here's a thread and sample code. https://developer.salesforce.com/forums/?id=906F00000008n0zIAA

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('/00O500000000000?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.setSubject('Report');
        message.setPlainTextBody('The report is attached.');
        message.setToAddresses( new String[] { '[email protected]' } );
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );
    }
}
2
  • thanks! I create a new class and copied/pasted the exact code (changed reportID, and seTToAddress). I scheduled the report. Got an email with attachment. However the attached csv is still the same as shown in the screenshot in my original question. I wonder is it about some setting in my outlook. Can you please execute the sample code and check if you are seeing the data in attached csv.? Commented Oct 14, 2020 at 21:58
  • Got it!!!! I read through all the discussions in the reference you provided. I replaced the ApexPages.PageReference... with following and i get the data in the attached csv. ApexPages.PageReference report = new ApexPages.PageReference('/REPORT_ID?csv=1&exp=1&enc=UTF-8&isdtp=p1'); // REPORT_ID = 'id of the report' Basically, this line works with lightning. Thanks JIM!!!!!! Commented Oct 14, 2020 at 22:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .