DMLOptions.DuplicateRuleHeader Class

Determines options for using duplicate rules to detect duplicate records. Duplicate rules are part of the Duplicate Management feature.

Namespace

Database

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

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.
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

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.
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!');
}