5
votes

I have an APEX class that is used to send an email out each 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[]{'[email protected]'});
      Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});     
    }
}

When I execute the send() method via an instance of the ReportBroadcaster via anonymous APEX, it is delivered as expected. However, when I schedule the class, the email is delivered with a blank body. If I switch the email body to plain text, it delivers fine (but that doesn't work for me).

How do I make this work?

UPDATE:

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

FINAL UPDATE: This is how to send HTML emails from scheduled APEX:

  • Create a class that implements the Schedulable interface.
  • Have the execute() method call an @future method.
  • Have the @future method call a web service enabled method in the class that sends the email.

While this approach is roundabout, it works.

3
Out of interest, does the running user have access to the page used?Matt Lacey
Yes. I'm running as System Admin and have access to the page.barelyknown
Are you calling the webservice method using HTTP request or directly from the @future method? Can you please paste a code example?Boris Bachovski
I just tried this. Scheduled class' execute() > @future > webservice method. The PDF gets sent, but its empty.Vid L
Did anybody find a solution to this problem. I am running into the same issue.Richard N

3 Answers

5
votes

getContent() method is not supported in scheduled Apex. See the last line of this page:

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

1
votes

I do not know of the top of my head why this doesnt work (it should), but I can maybe suggest a workaround.

You can convert your vforce page into vforce Email Template (or create a new based on the old if you are also using the page somewhere else) and then use that template as the source for your email. Key points to check in the documentation are SingleEmailMessage.setTemplateId in apex docs and <messaging:*> components in vforce docs.

1
votes

I also faced same problem and was able to find Workaround. I have documented my solution here and hope it will help others.

http://www.shivasoft.in/blog/salesforce/apex/send-email-with-generated-pdf-as-attachment-from-trigger/

Regards, Jitendra Zaa