-
Notifications
You must be signed in to change notification settings - Fork 556
Open
Description
Is your feature request related to a problem? Please describe.
There are problems when using the unit of work in classes where multiple callouts are expected within the same transaction, causing this error to appear:
"All active Savepoints must be released before making callouts".
Describe the solution you'd like
It looks like a simple fix; however, some unit tests are failing.
This is something I came up with:
public void commitWork() {
Savepoint sp = Database.setSavepoint();
Boolean wasSuccessful = false;
try {
doCommitWork();
wasSuccessful = true;
} catch (Exception e) {
Database.rollback(sp);
throw e;
} finally {
if (wasSuccessful && !Test.isRunningTest()) {
Database.releaseSavepoint(sp);
}
doAfterCommitWorkSteps(wasSuccessful);
}
}
I added myself the finally if-block:
if (wasSuccessful && !Test.isRunningTest()) {
Database.releaseSavepoint(sp);
}
I consider Test.isRunningTest() in this scenario a bad practice since this is used to bypass unit tests.
Additional context
Release Savepoint Documentation
Make Callouts After Rolling Back DML and Releasing Savepoints
KatiaDev
Metadata
Metadata
Assignees
Labels
No labels