This critical update:
PageReference getContent() and getContentAsPDF() Methods Treated as Callouts
will be auto-activated in January 2016.
We have some code that uses a queue of custom event objects where listener classes can be registered that both consume those events (in one case that means making getContentAsPDF
calls) and can insert additional events. With the critical update turned on that can result in the:
You have uncommitted work pending. Please commit or rollback before calling out.
error because:
- a PDF can be generated (a callout)
- then some events inserted (DML)
- then another PDF can be generated (a callout) that produces the error.
Future calls can't be chained so they are out.
With the encouragement of Make Calls to PageReference.getContent() in Asynchronous Apex that mentions Queueable
a chained Queueable
looked like the way to go:
public static void fire() {
String requestId = insertEvents();
if (requestId != null) System.enqueueJob(new FireQueueable(requestId));
}
public class FireQueueable implements Queueable, Database.AllowsCallouts {
private String requestId;
public FireQueueable(String requestId) {
this.requestId = requestId;
}
public void execute(QueueableContext context) {
if (fireFor(requestId)) {
String nextRequestId = insertEvents();
if (nextRequestId != null) System.enqueueJob(new FireQueueable(nextRequestId));
}
}
}
but that generates:
System.AsyncException: Maximum callout depth has been reached.
as described in Can we callout and chain a Queueable class? and Allow callouts from chained queueable Apex jobs.
I'm starting to think there is no solution to this problem. Can anyone suggest a solution?