I am receiving the below error when the batch is executing. This is not happening all the time. This happens only at certain times.

uncommitted work pending. Please commit or rollback before calling out

The batch that runs performs deletion of some child records based on the flag for the passed in parent record in the batch. Then it makes a callout to receive token and then another callout to send both parent and child record. For each callout, whatever response is received that is stored in a list of log object. Then after processing all the records for callout, insert of the log object is done. Then the processed parent and child records flag is reset by update.

I have scheduled this batch to run every 3 minutes. In that only some logs have success and other logs have empty responses because of the error. Can someone help me on this?

  • are you doing dml operation and callout in the same transaction ? – anmrk May 15 '17 at 17:31
  • The problem is with the optional deletion DML that occurs before the callout is performed. See System.CalloutException: You have uncommitted work pending – Daniel Ballinger May 15 '17 at 19:59
  • Once that deletion is done, and that final result is only passed to the callout class – ləːnə May 16 '17 at 3:20
  • I found the root cause , when token callout is done, dml is also there in the same transaction. It looks like the issue is because of that – ləːnə May 16 '17 at 3:21