+ Start a Discussion
Doug ACFDoug ACF 

Visualforce within Scheduled Apex

We have a working process where the user submits a Visualforce form which causes a Visualforce PDF to be created and emailed to the user.  We want the same PDF generation/email to happen on a daily schedule.  So we have made some changes to invoke similar functionality via the Apex Scheduler.  The scheduled class runs without any error messages and a PDF email gets sent, but the PDF is unable to be opened (indicating that something went wrong in the Visualforce rendering of it).


We have tested the functionality using anonymous execute and it works fine.  Seems to be a problem related to it being run by the scheduler.  Is there a limitation where you can't invoke Visualforce (new PageReference / getContent()) from within a scheduled Apex class?

The docs say (in various places) that you can't use these in triggers, batch jobs, test methods and @future methods.  This is pretty restrictive so it wouldn't surprise me if it was also excluded from scheduled apex too, though I can't find a definitive statement.
Doug ACFDoug ACF
Yep that seems to be the case.  Could someone from Salesforce confirm that?  And if so please update the docs regarding getContent and Apex Scheduler to specifically call that out.

As noted above, getContentAsPDF is not supported for scheduled Apex. We'll be updating the docs to state this more clearly. Thanks for bringing this to our attention.