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
Database.DMLOptions dml = new Database.DMLOptions();
dml.DuplicateRuleHeader.allowSave = true;
dml.DuplicateRuleHeader.runAsCurrentUser = true;
Account duplicateAccount = new Account(Name='dupe');
Database.SaveResult sr = Database.insert(duplicateAccount, dml);
if (sr.isSuccess()) {
System.debug('Duplicate account has been inserted in Salesforce!');
}
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.
Database.DMLOptions dml = new Database.DMLOptions();
dml.DuplicateRuleHeader.allowSave = true;
dml.DuplicateRuleHeader.runAsCurrentUser = true;
Account duplicateAccount = new Account(Name='dupe');
Database.SaveResult sr = Database.insert(duplicateAccount, dml);
if (sr.isSuccess()) {
System.debug('Duplicate account has been inserted in Salesforce!');
}
runAsCurrentUser
Make sure that sharing rules for the current user are enforced when
duplicate rules run by setting this property to true.
Use the sharing rules specified in the class for the request by setting this property to
false. If no sharing rules are specified, Apex code
runs in system context and sharing rules for the current user are not
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. Typically, lead conversion Apex code runs in a system context and does not enforce sharing rules for the current user.
Example
This example shows how to set options so that duplicate rules run for the current user when
saving a new
account.
Database.DMLOptions dml = new Database.DMLOptions();
dml.DuplicateRuleHeader.allowSave = true;
dml.DuplicateRuleHeader.runAsCurrentUser = true;
Account duplicateAccount = new Account(Name='dupe');
Database.SaveResult sr = Database.insert(duplicateAccount, dml);
if (sr.isSuccess()) {
System.debug('Duplicate account has been inserted in Salesforce!');
}