Make Calls to PageReference.getContent() in Asynchronous Apex

You can now make calls to the getContent() and getContentAsPdf() methods of the PageReference class from within asynchronous Apex such as Batch Apex, Schedulable and Queueable classes, and @future methods. This allows you to design much more flexible and scalable services that, for example, render Visualforce pages as PDF files.

For the purposes of limits and restrictions, calls to getContent() (and getContentAsPdf(), which behaves the same) are treated as callouts. The behavior and restrictions vary depending on the PageReference it’s called on, and in certain cases are relaxed. There are three different possibilities.

PageReference instances that explicitly reference a Visualforce page
For example, Page.existingPageName.

Calls to getContent() aren’t subject to either the maximum concurrent callouts limit or the maximum total callouts per transaction limit.

Additionally, only PageReference instances that reference a Visualforce page can call getContent() from scheduled Apex, that is, Apex classes that implement the Schedulable interface.

PageReference instances that reference a Salesforce URL
For example, PageReference('/' + recordId).

Calls to getContent() for Salesforce URLs aren’t subject to the maximum concurrent callouts limit, but they are subject to the maximum total callouts per transaction limit.

You can’t call getContent() for Salesforce URLs from within scheduled Apex.

PageReference instances that reference an external URL
For example, PageReference('https://www.google.com/').

Calls to getContent() for external URLs are subject to both the maximum concurrent callouts limit and the maximum total callouts per transaction limit.

You can’t call getContent() for external URLs from within scheduled Apex.

Note

Note

Even when calls to getContent() and getContentAsPdf() aren’t tracked against callout limits, they’re still subject to the usual Apex limits such as CPU time, etc. These limits are cumulative across all callouts within the originating transaction.

Finally, we’ve relaxed the restrictions on calling getContent() and getContentAsPdf() after performing DML operations (excluding creating Savepoints). If the calls to getContent() and getContentAsPdf() are internal calls, they’re now allowed. External callouts after DML operations are still blocked, and you still can’t make callouts from within getContent() calls themselves, that is, during the rendering of the page.