DMLOptions.DuplicateRuleHeader Class
Determines options for using duplicate rules to detect duplicate
records. Duplicate rules are part of the Duplicate Management feature.
Namespace
Example
The following example shows how to save an account record that’s been identified as a
duplicate. To learn how to iterate through duplicate errors, see DuplicateError Class
1Database.DMLOptions dml = new Database.DMLOptions();
2dml.DuplicateRuleHeader.allowSave = true;
3dml.DuplicateRuleHeader.runAsCurrentUser = true;
4Account duplicateAccount = new Account(Name='dupe');
5Database.SaveResult sr = Database.insert(duplicateAccount, dml);
6if (sr.isSuccess()) {
7 System.debug('Duplicate account has been inserted in Salesforce!');
8}DMLOptions.DuplicateRuleHeader Properties
The following are properties for DMLOptions.DuplicateRuleHeader.
allowSave
For a duplicate rule, when the Alert option is enabled, bypass alerts
and save duplicate records by setting this property to true. Prevent duplicate records from being saved by setting this property to
false.
Signature
public Boolean allowSave {get; set;}
Property Value
Type: Boolean
Example
This example shows how to save an account record that’s been identified as a duplicate.
dml.DuplicateRuleHeader.allowSave = true means the
user should be allowed to save the duplicate. To learn how to iterate through duplicate
errors, see DuplicateError Class.
1Database.DMLOptions dml = new Database.DMLOptions();
2dml.DuplicateRuleHeader.allowSave = true;
3dml.DuplicateRuleHeader.runAsCurrentUser = true;
4Account duplicateAccount = new Account(Name='dupe');
5Database.SaveResult sr = Database.insert(duplicateAccount, dml);
6if (sr.isSuccess()) {
7 System.debug('Duplicate account has been inserted in Salesforce!');
8}runAsCurrentUser
Determine whether sharing rules for the current user are enforced
when duplicate rules run (true) or not (false). If no sharing rules are specified, Apex code runs in
user mode and sharing rules for the current user are enforced.
Signature
public Boolean runAsCurrentUser {get; set;}
Property Value
Type: Boolean
Usage
If specified as true, duplicate rules run for the current user, which ensures users can’t view duplicate records that aren’t available to them.
Use runAsCurrentUser = true to detect duplicates when converting leads to contacts.
Example
This example shows how to set options so that duplicate rules run for the current user when
saving a new
account.
1Database.DMLOptions dml = new Database.DMLOptions();
2dml.DuplicateRuleHeader.allowSave = true;
3dml.DuplicateRuleHeader.runAsCurrentUser = true;
4Account duplicateAccount = new Account(Name='dupe');
5Database.SaveResult sr = Database.insert(duplicateAccount, dml);
6if (sr.isSuccess()) {
7 System.debug('Duplicate account has been inserted in Salesforce!');
8}