Sub topic 8
The record is saved but not yet committed
This chapter assumes you can write a basic Apex trigger, know what DML is, and have read the earlier chapters on before triggers, validation rules, and duplicate rules. It covers the step after those checks pass. Salesforce writes the record to the database but keeps the transaction open.
What "saved" means here
After the before triggers, custom validation rules, and duplicate rules, Salesforce saves the record to the database. It does not commit. Commit is a separate step at the very end of the transaction, covered in a later chapter.
An uncommitted save has these properties:
- The row exists inside your transaction, so queries in the same transaction can see it.
- Other users and other transactions cannot see it.
- The record holds a lock, so other transactions that try to edit it must wait or fail.
- An error anywhere later in the transaction undoes the save.
What changes at this step
For an insert, the record gets its Id here. That is why Trigger.new[i].Id is null in a before insert trigger and populated in an after insert trigger. The save also computes system-generated values such as CreatedDate and LastModifiedDate.
Formula fields are not stored. They are calculated when the record is read. In an after trigger, the records in Trigger.new show saved values and the new Id, but they are read-only. To change a field on the same record after the save, you must query it again and run a DML update. That update fires the update triggers again.
| Aspect | Before the save | After the save, before commit |
|---|---|---|
| `Id` on insert | `null` | Populated |
| Edit `Trigger.new` fields directly | Allowed | Not allowed (read-only) |
| Visible to a SOQL query in this transaction | Old values only | New values |
| Visible to other users | No | No |
| Undone by an error | Nothing to undo yet | Yes, rolled back |
Seeing the uncommitted row
You can confirm the behavior in Anonymous Apex. This script inserts an account, queries it, rolls back to a savepoint, and queries again.
Savepoint sp = Database.setSavepoint();
Account a = new Account(Name = 'Temp Co');
insert a;
System.debug('Id after insert: ' + a.Id);
Integer inTransaction = [SELECT COUNT() FROM Account WHERE Id = :a.Id];
System.debug('Rows visible in this transaction: ' + inTransaction);
Database.rollback(sp);
Integer afterRollback = [SELECT COUNT() FROM Account WHERE Id = :a.Id];
System.debug('Rows after rollback: ' + afterRollback);The first count is 1 and the second is 0. The row was real inside the transaction but never became permanent.
After Database.rollback, the variable a still holds its old Id. The database no longer has that row. Inserting a again fails, so create a new Account instead.
Using the saved state in an after trigger
An after trigger sees the saved record. This trigger queries the new rows and relies on that behavior.
trigger AccountAudit on Account (after insert) {
Set<Id> ids = Trigger.newMap.keySet();
List<Account> saved = [
SELECT Id, Name, CreatedDate
FROM Account
WHERE Id IN :ids
];
List<Task> tasks = new List<Task>();
for (Account a : saved) {
tasks.add(new Task(
Subject = 'Review new account ' + a.Name,
WhatId = a.Id
));
}
insert tasks;
}The query is outside any loop, so it is bulk-safe. It counts as one SOQL query against the transaction's limit. For fields already in Trigger.new, you can skip the query. Query only when you need related data or fields that the trigger context does not provide.
Locks and concurrent edits
Because the saved row is locked until commit, a long transaction increases the chance of UNABLE_TO_LOCK_ROW errors for other users. Any slow work after the save, such as heavy after trigger logic or many chained flows, keeps the lock longer. Keep the post-save work short.
The same applies to parent records. When your transaction updates a child, Salesforce may lock the parent too. Large data loads that mix many children of the same parent in parallel batches are a common source of lock errors.
Callouts after an uncommitted save
Apex cannot make an HTTP callout while uncommitted work is pending in the transaction. The platform rejects the callout with a CalloutException that says you have uncommitted work pending.
This is the practical reason to know where the save sits. By the time any after trigger runs, uncommitted work already exists.
Common mistakes
- Trying to set
a.Description = 'x'on a record in an after trigger. This throws a "record is read-only" error. Use a before trigger, or query the record and update a copy. - Assuming other users can see the new record because your trigger saw it. They cannot until the commit.
- Assuming a rolled-back transaction leaves limits untouched. Limits consumed stay consumed.
- Reusing an sObject variable after a rollback and expecting a fresh insert.
Exercise
An after insert trigger on Contact makes an HTTP callout to an external service and fails with an error about uncommitted work. Why does this happen, and what is a safe fix?
Show answer
By the time an after trigger runs, the record has already been saved to the database without a commit, so the transaction has uncommitted work pending. Apex does not allow callouts in that state. The fix is to move the callout into asynchronous Apex, such as a Queueable class that implements Database.AllowsCallouts. The trigger enqueues the job, and the job runs in its own transaction after the commit.
Exercise
In Anonymous Apex you insert an Account, then call Database.rollback to a savepoint taken before the insert. A SOQL query for the account's Id then returns 0 rows. What happened to the Id on your variable, and what is the correct way to insert the account again?
Show answer
The variable keeps its old Id, but the row no longer exists in the database. Inserting the same variable again fails. Create a new Account object and insert that one.