Savepoints, partial success and custom exceptions
What Database.rollback does and doesn't revert, how to call out after a rollback, how allOrNone false retries and re-fires triggers, how to design and chain custom exceptions, and how to log errors that survive a rolled-back transaction.
Describe and use the Apex features available for error handling and maintaining transactional integrity.
~7 min read · 10 practice questions ·
What the exam expects
- Predict the database and variable state after
Database.rollback(): discarded DML, surviving static variables, Ids left on sObjects. - Make a callout after rolling back, using
Database.releaseSavepoint(). - Choose
Database.insert(records, false)to keep valid records and read failures fromSaveResult, and explain why triggers re-fire on the retry. - Design custom exceptions: naming, inherited constructors, chaining a cause, catching by parent type.
- Recognise what can't be caught (
LimitException) and log errors so the log survives a rollback.
Key terms: savepoint (a marker of database state that later DML can be rolled back to); allOrNone (the Database-method flag; true by default, false allows partial success); SaveResult (the per-record result of a Database insert or update); chained cause (an exception passed to another exception's constructor); Publish Immediately (a platform event behaviour that publishes even if the transaction fails).
Key facts
- Detail
- Not reverted
- Detail
- An sObject inserted after the savepoint keeps its Id; re-inserting it fails
- Detail
- setSavepoint and rollback count toward DML statements, not rows
- Detail
- From API 60.0, allowed once uncommitted DML is rolled back and savepoints are released
- Detail
- true: one failure throws and saves nothing
- Detail
- Up to three; limits reset each retry; triggers re-fire on the clean subset
- Detail
- Must end with "Exception" and extend Exception
- Detail
- Can't be caught
- Detail
- 100 queries per transaction
- Detail
- Default; published even if the transaction fails
Sources for this section (7)
- Transaction Controldeveloper.salesforce.com (opens in a new tab)
- DML Statements vs. Database Class Methodsdeveloper.salesforce.com (opens in a new tab)
- Bulk DML Exception Handlingdeveloper.salesforce.com (opens in a new tab)
- Create Custom Exceptionsdeveloper.salesforce.com (opens in a new tab)
- Exception Class and Built-In Exceptionsdeveloper.salesforce.com (opens in a new tab)
- Governor Limitsdeveloper.salesforce.com (opens in a new tab)
- Define a Platform Eventdeveloper.salesforce.com (opens in a new tab)
- Why does inserting the same sObject variable again after a rollback fail?
- When does a partial-success insert run a trigger more than once?
- Which constructor chains a caught exception into a custom one?
- Why does a Publish Immediately platform event suit error logging?
The questions are free to read. Answers with their sources and self-grading are included with the pack.
Keep reading with the Platform Developer II pack
- Core concepts
- Exam traps
- Worked scenario
The pack includes:
- All 28 lessons of the Platform Developer II guide, with reading progress
- Interactive decision walks and step-by-step sequence steppers
- Inline practice questions and one-click topic practice (10 questions for this lesson)
- An official source cited for every claim
- An AI tutor that answers from the guide
- Unlimited timed practice exams