So, I've heard that you cannot do any Callouts after a DML operation in Apex. So, I've designed my app this way (some pseudo-code follows):
String url;
List<Object> chunk;
for (Integer page=1; page<10; page++) {
url = '/responses?per_page=' + PER_PAGE + '&page=' + page;
// Callout happens inside client.get():
chunk = (List<Object>)JSON.deserializeUntyped(client.get(url));
// all DML is in here
ProcessRecordsJob processJob = new ProcessRecordsJob(chunk);
System.enqueueJob(processJob);
}
From what I know, a queued job must have an isolated execution context. So, why do I get "Uncommitted work pending"?
Note: it's not in the test (there is not real isolation in tests, but that's different issue); it works this way on the prod…
Note 2: I can see in the logs that the error happens after it tries to execute "page=2" (i.e. second) callout request.
Could the System.enqueueJob()
itself produce that DML, after which I can't do no more callouts?