1

I am getting uncommitted work pending issue in my test class but Limits.getDMLStatements() says there are 0 DML performed before and after callout. What else could cause this issue?

Code is running in Batch context inside execute method

try {
    System.debug(Limits.getDMLStatements()); //log: 0
    restclient.makeCallout(somedata);
}
catch(Exception ex){
   System.debug(Limits.getDMLStatements()); //log: 0
   System.debug(ex.getMessage()); //log: uncommitted work pending 
}

NOTE: I am getting this issue in my packaging org but scratch org test are passing.

1 Answer 1

5

Database.executeBatch method modifies the state of the database(Creates an Apex Job record), so for purposes of callouts, count as a DML operation.

This is the reason you are getting uncommitted work pending issue in the callout exception.

As a workaround, you can directly call the execute method of batch class in a test context

Example

if(Test.isRunningTest()) {
    batchInstance.execute(null, listOfRecords);
} else {
    Database.executeBatch(batchInstance, 10);   
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .