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:
While this approach is roundabout, it works.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With