Newer Version Available
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
Set to true to save the
duplicate record. Set to false to prevent the
duplicate record from being saved.
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
Set to true to make sure
that sharing rules for the current user are enforced when duplicate rules run. Set to false to use the sharing rules specified in the class for
the request. 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.
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}