Sub topic 7
Duplicate Rules and the Uncommitted Save
This chapter assumes you can write a basic Apex trigger, know what validation rules do, and understand DML (insert, update, delete). It covers two steps of the order of execution: duplicate rules, and the point where the record is saved but not yet committed.
Duplicate rules and blocking saves
After custom validation rules pass, Salesforce runs duplicate rules. A duplicate rule compares the incoming record with existing records, using matching rules, and decides what to do when it finds a likely match.
A duplicate rule can take one of two actions:
- Block stops the save and shows an error.
- Allow lets the save continue. The user can see an alert, and Salesforce can log the match in a duplicate record set.
If the rule blocks, the transaction stops here. Everything done so far is rolled back. That includes any field changes your before triggers made.
Duplicate rules run only if the earlier steps succeeded. A record that failed a validation rule never reaches this step.
Apex DML can bypass the rule. Use the DMLOptions.DuplicateRuleHeader class and set allowSave = true:
Account a = new Account(Name = 'Acme');
Database.DMLOptions opts = new Database.DMLOptions();
opts.duplicateRuleHeader.allowSave = true;
Database.SaveResult sr = Database.insert(a, opts);
System.debug('Saved: ' + sr.isSuccess());Without this option, a blocking rule makes the DML statement fail. With Database.insert and allOrNone left at its default, the error is thrown. Pass false for allOrNone to get a failed SaveResult instead.
The record is saved but not yet committed
After duplicate rules, Salesforce writes the record to the database. It does not commit yet. The write is held inside the open transaction.
This has three effects:
- The Id exists. For an insert, the new record now has an Id. That is why
after inserttriggers can usea.Id, andbefore inserttriggers cannot. - Other users cannot see the record. Until the commit, other transactions do not see the change.
- The save can still be undone. If an after trigger, a flow, or a later step throws an error, Salesforce rolls back the whole transaction. The record disappears as if it was never saved.
The following trigger shows both sides of this:
trigger AccountFollowUp on Account (after insert) {
List<Task> tasks = new List<Task>();
for (Account a : Trigger.new) {
tasks.add(new Task(Subject = 'Follow up', WhatId = a.Id));
}
insert tasks;
}The WhatId works because the Account has an Id. If the insert tasks line fails, the Account insert is also undone.
Common errors
- Expecting a duplicate rule to run on a record that failed validation. It does not.
- Using
Idin abefore inserttrigger. The Id is not assigned until this save step. - Assuming a saved record is visible to other users or to outside systems before the commit.
Exercise
A before insert trigger sets a field, and a blocking duplicate rule then finds a match. Is the field change kept? Why does an after insert trigger see an Id but a before insert trigger does not?
Show answer
The field change is not kept. A blocking duplicate rule stops the save and rolls back the whole transaction. The Id is assigned when the record is written to the database, which happens after duplicate rules and before after triggers. Before triggers run earlier, so no Id exists yet.