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
Mustafa TopMustafa Top 

Why does getContent() not work when it is schedule?

I am working on developer console.

I get mail with attachment when I try to execute this: SendingReportTest.execute(null);

But I don't get the attachment in the mail I try to execute this: System.schedule('JOB1',   '0 0 * * * ?',   new SendingReportTest());

Attachment is empty. 

 

My schedulable class is following:

global class SendingReportTest implements Schedulable{

    public static void execute(SchedulableContext s){
        sendMail();
    }

    public static void sendMail() {

        try{
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            ApexPages.PageReference report = new ApexPages.PageReference('/00Og0000000JSg4?excel=1&exp=1&enc=iso-8859-9');
            Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
            attachment.setFileName('report.xls');
            attachment.setBody(report.getContent());
            attachment.setContentType('text/csv');
            mail.setFileAttachments(new Messaging.EmailFileAttachment[]{ attachment });
            mail.setToAddresses(new String[]{test@email'});
            mail.setSubject('konu');
            mail.setPlainTextBody('text');
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }catch(Exception e){system.debug(e);}
    }

}

Sonam_SFDCSonam_SFDC

Hi Mustafa,

 

This is suggested as one of the best practises for using Apex Scheduler:

  • You can't use the getContent and getContentAsPDFPageReference methods in scheduled Apex.

reference:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

Mustafa TopMustafa Top
Hi Sonam,

Yes, I've seen this. What should I do for sending a report xls file as attachment? Is there a way to do it ?