Sub topic 1
A save is one transaction
This chapter introduces the idea that everything Salesforce does when you save a record happens inside a single transaction. Later chapters walk through each step in order. Here you build the model that makes those steps make sense.
Prerequisites
This chapter assumes you have:
- Basic Apex syntax, and you can write a simple trigger.
- Familiarity with Salesforce objects, fields, and records.
- Awareness of declarative automation such as validation rules and Flow.
- An understanding of DML operations (insert, update, delete).
What a transaction is
A transaction is a group of operations that either all succeed or all fail together. When a user clicks Save, an API client sends a record, or Apex runs insert, Salesforce starts one transaction. Every step it performs for that save belongs to that transaction:
- System validation.
- Before-save flows and before triggers.
- Custom validation rules.
- The write to the database.
- After triggers, workflow, and after-save flows.
- Updates to related records caused by any of the above.
If any step fails with an unhandled error, Salesforce rolls back the whole transaction. The database returns to the state it had before the save started. Nothing is kept from the steps that ran earlier.
Saved is not committed
The database write and the commit are two different moments. This distinction explains most of the behavior you will see later.
- Saved to the database: the record is written inside the transaction. Your after trigger can read it, query it, and use its
Id. Other users cannot see it yet. - Committed: the transaction finishes successfully and the changes become permanent and visible to others.
Between those two moments, Salesforce can still undo everything. That is why an after trigger that throws an error removes a record that was already "saved".
Seeing the boundary in code
The next example shows one transaction doing several things. It inserts an account, then forces an error. Because the error is unhandled, the account is not kept.
Account a = new Account(Name = 'Transaction Demo');
insert a;
System.debug('Saved, Id = ' + a.Id);
// The Id exists and the record is readable here, but it is not committed.
Integer count = [SELECT COUNT() FROM Account WHERE Name = 'Transaction Demo'];
System.debug('Visible inside the transaction: ' + count);
// An unhandled exception ends the transaction.
Integer boom = 1 / 0;Run this as anonymous Apex. The first two debug lines show the account exists inside the transaction. After the divide-by-zero error, query for Name = 'Transaction Demo' again in a new execution. You will find no record. The insert was rolled back.
What counts as one transaction
A transaction is bounded by the entry point, not by the number of DML statements.
| Entry point | What is in the transaction |
|---|---|
| A user saves a record in the UI | The save and all automation it triggers |
| An API call with one record | The same, for that one record |
| An API call with a batch of records | One chunk of records, with their automation |
| An Apex `insert` in a trigger | Part of the transaction that is already running |
| A call to `Database.insert` | The current transaction, unless you start a new context |
Two details matter here:
- DML issued from inside a trigger or flow does not start a new transaction. The new save, with its own order of execution, runs nested inside the current one.
- Asynchronous Apex (
@future, Queueable, Batch) runs in its own separate transaction later. An error there does not roll back the transaction that queued it.
Why this model matters
Three consequences follow from the one-transaction model. Each gets its own chapter later.
- Errors roll back everything. A failing validation rule, a failing trigger, or a failing flow on a related record can undo the original save.
- Limits are shared. Governor limits, such as the number of SOQL queries and DML statements, apply to the whole transaction. Every trigger, flow, and managed package draws from the same budget.
- Automation can nest. A save can cause another save, which runs its own full sequence inside the same transaction.
Partial success and savepoints
You can control rollback to a degree. Two tools exist.
First, Database.insert(records, false) allows partial success. Records that fail are reported in the results, and the successful ones are kept. Each record that fails is rolled back on its own.
Second, a savepoint lets you roll back part of a transaction yourself.
Savepoint sp = Database.setSavepoint();
try {
insert new Account(Name = 'Maybe Keep');
// Other work that might fail
throw new DmlException('Something went wrong');
} catch (Exception e) {
Database.rollback(sp);
System.debug('Rolled back to the savepoint: ' + e.getMessage());
}The account is discarded because of Database.rollback(sp). The transaction itself continues and can still commit other work. Note that rolling back does not reset the values of static variables or the Id already assigned to an in-memory record. Treat those variables as stale after a rollback.
Common mistakes
- Treating "saved" as "committed". Making a callout or sending an email assuming the data is final can leave other systems out of sync if the transaction later rolls back.
- Catching an exception and expecting a rollback. If you catch an error, the transaction does not roll back unless you do it yourself or another error occurs.
- Assuming each record is isolated. In bulk operations, one failure can fail the whole chunk unless you use partial success.
Summary
A record save is a single transaction. Every validation, trigger, flow, and related update runs inside it. The database write happens before the commit, so work can still be undone. An unhandled error at any step rolls back everything. The next chapter looks at the first step inside that transaction: how Salesforce loads the record and applies new values.
Exercise
An after insert trigger on Account inserts a Task. The Task insert succeeds, but a later validation rule on a related record fails and throws an unhandled error. What happens to the Account and the Task?
Show answer
Both are rolled back. The Account save, the Task insert, and all other changes belong to the same transaction. An unhandled error at any step rolls back the whole transaction, so neither record is committed.
Exercise
Your Apex catches a DmlException from an insert and logs it, then continues. Is the earlier work in the transaction rolled back automatically?
Show answer
No. Catching the exception lets the transaction continue. Only an unhandled error rolls back everything. To undo earlier work yourself, use Database.setSavepoint() before the work and Database.rollback() in the catch block.