Sub topic 14
What happens when something fails
This chapter assumes you can write a basic Apex trigger, know validation rules and Flow, and understand DML (insert, update, delete). It covers what happens to a save when any step of the order of execution raises an error.
The whole transaction rolls back
A record save is one transaction. If any step throws an unhandled exception or fails validation, Salesforce rolls back every change made in that transaction. The rollback covers the original record, records created in triggers, flow updates, and parent saves. Nothing is committed, and the user or API caller receives the error.
This applies to every step, including steps that run after the record was written to the database. The earlier chapter on saving without committing explains why: the database write is provisional until the commit.
Failing on purpose with addError
In a trigger, addError() marks a record or field as failed and blocks the save. Use it for checks that a validation rule can't express.
trigger OpportunityGuard on Opportunity (before update) {
for (Opportunity o : Trigger.new) {
if (o.StageName == 'Closed Won' && o.Amount == null) {
o.addError('Amount is required to close this opportunity.');
}
}
}In bulk operations, the behavior depends on how the DML was called:
- A plain
insertorupdatestatement is all-or-nothing. One failed record rolls back the whole statement. Database.insert(records, false)allows partial success. Records that fail are reported in theSaveResultlist, and the others are saved.
Catching exceptions does not undo earlier work
A try/catch block stops an exception from propagating, but changes made before the exception stay in the transaction. If you catch an error and the transaction continues, the earlier DML will still commit. Use a savepoint when you want to undo part of the work.
Savepoint sp = Database.setSavepoint();
try {
insert new Account(Name = 'Demo Account');
insert new Contact(LastName = 'Demo'); // fails if the field requirements are not met
} catch (DmlException e) {
Database.rollback(sp);
System.debug('Rolled back: ' + e.getMessage());
}What does not roll back
Some effects leave the transaction and can't be undone:
- Emails sent with
Messaging.sendEmailare not recalled if the transaction later fails, unless the failure happens before the send is committed. Check your org's behavior in testing. - Callouts to external systems have already reached the other system.
- Auto-number values are consumed, so gaps can appear in the sequence.
Common mistakes
- Swallowing exceptions with an empty
catchblock, which hides the failure while earlier DML is still committed. - Assuming a failed after trigger leaves the record saved. It doesn't, because the commit never happened.
- Doing a callout before DML in the same transaction and expecting it to reverse on failure.
Exercise
An after update trigger on Account throws an unhandled exception after a workflow field update has already changed the record. Is the field update saved?
Show answer
No. The unhandled exception rolls back the whole transaction, including the workflow field update and any other changes made earlier in it. The user sees the error and no data is committed.
Exercise
You call Database.update(accounts, false) with 200 records, and 3 fail validation. What happens to the other 197?
Show answer
They are saved. The false argument allows partial success, and the 3 failures are reported in the returned SaveResult list.