Why are HTML emails sent by the APEX Schedulable class, which comes with an empty body?

I have an APEX class that is used to send emails every day at 7pm:

global class ReportBroadcaster implements Schedulable {

    global ReportBroadcaster () {
    }

    global void execute (SchedulableContext sc) {
      send ();
    }

    global void send () {
      PageReference page = new PageReference ('/ apex / nameofvfpage');
      page.setRedirect (true);
      Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage ();
      email.setSubject ('Example Subject');
      email.setHtmlBody (page.getContent (). toString ());
      email.setToAddresses (new String [] {' test@test.com '});
      Messaging.sendEmail (new Messaging.SingleEmailMessage [] {email});     
    }
}

When I execute the send () method through an instance of ReportBroadcaster through anonymous APEX, it is delivered as expected. However, when I plan the class, the email is delivered with an empty body. If I switch the body of the email to plain text, it will give a fine (but this does not work for me).

How do I do this job?

UPDATE:

You cannot call getContent () on PageReference instances from the planned APEX or @future methods (I'm not sure why this would be, but this is what it is). I think the solution would be to create a web service that I will call from the @future method. It seems incredibly hacked, but I'm not sure what else I could do.

FINAL UPDATE: Here's how to send HTML messages from a scheduled APEX:

  • , Schedulable.
  • execute() @future.
  • @future , -, , .

, .

+5
3

, - , ( ), , , .

vforce vforce Email ( , - ), . SingleEmailMessage.setTemplateId apex docs <messaging:*> vforce.

+1

All Articles