Sub topic 15
Handling duplicate errors and bypassing rules in DML
This chapter assumes you know basic Apex, how Database.insert returns a SaveResult, and how a duplicate rule's action (Allow, Block, alert, report) is configured. Here you will see what happens to DML when a duplicate rule fires, and how code can react.
How duplicate rules affect DML
Duplicate rules run on inserts and updates made from Apex, the API, and integrations, not only from the UI. What happens depends on the rule's action:
- Block: the save fails. No code can override this.
- Allow with alert: the save fails by default, and the caller can retry with an option that acknowledges the alert.
- Allow with report only: the save succeeds and the match is recorded in a duplicate record set.
A plain insert statement throws a DmlException when a rule stops the save. Use the Database methods with allOrNone set to false when you want to inspect the failure instead.
Reading a duplicate error
When a save fails because of a rule, the error in the SaveResult is a Database.DuplicateError. It carries the rule that fired and the matches it found.
Account acct = new Account(Name = 'Acme Corporation', BillingCity = 'Chicago');
Database.SaveResult saveResult = Database.insert(acct, false);
if (!saveResult.isSuccess()) {
for (Database.Error err : saveResult.getErrors()) {
if (err instanceof Database.DuplicateError) {
Database.DuplicateError dupError = (Database.DuplicateError) err;
Datacloud.DuplicateResult dupResult = dupError.getDuplicateResult();
System.debug('Blocked by rule: ' + dupResult.getDuplicateRule());
}
}
}Check the error type with instanceof because a failed save can also contain unrelated errors, such as validation rule failures.
Acknowledging an alert
For rules set to Allow with alert, set allowSave on a DMLOptions object. This tells Salesforce that the caller has seen the alert and wants to save anyway.
Account acct = new Account(Name = 'Acme Corporation', BillingCity = 'Chicago');
Database.DMLOptions dmlOptions = new Database.DMLOptions();
dmlOptions.DuplicateRuleHeader.allowSave = true;
Database.SaveResult saveResult = Database.insert(acct, dmlOptions);
System.debug('Saved despite alerts: ' + saveResult.isSuccess());Common mistakes
- Using a plain
insertand not catchingDmlException, so a duplicate stops the whole transaction. - Setting
allowSave = trueeverywhere. This silences the alerts you configured, so use it only where a person or process has reviewed the match. - Assuming a Block rule can be bypassed in code. Change the rule's action or conditions instead.
Exercise
A trigger inserts Accounts with Database.insert(acct, dmlOptions) and allowSave = true, yet some inserts still fail with a DuplicateError. What is the most likely cause, and how do you confirm it?
Show answer
The matching duplicate rule's action is Block, which allowSave cannot override. Call getDuplicateResult().getDuplicateRule() on the Database.DuplicateError to see which rule fired, then check its action in Setup.