DuplicateError Class

Contains information about an error that occurred when an attempt was made to save a duplicate record. Use if your organization has set up duplicate rules, which are part of the Duplicate Management feature.

Namespace

Database

Example

When you try to save a record that’s identified as a duplicate record by a duplicate rule, you’ll receive a duplicate error. If the duplicate rule contains the Allow action, an attempt will be made to bypass the error.
1// Try to save a duplicate account
2Account duplicateAccount = new Account(Name='Acme',  BillingCity='San Francisco');
3Database.SaveResult sr = Database.insert(duplicateAccount, false);
4if (!sr.isSuccess()) {
5	
6	// Insertion failed due to duplicate detected
7	for(Database.Error duplicateError : sr.getErrors()){
8		Datacloud.DuplicateResult duplicateResult = 
9                  ((Database.DuplicateError)duplicateError).getDuplicateResult();
10		System.debug('Duplicate records have been detected by ' + 
11                  duplicateResult.getDuplicateRule());
12		System.debug(duplicateResult.getErrorMessage());
13	}
14
15	// If the duplicate rule is an alert rule, we can try to bypass it
16	Database.DMLOptions dml = new Database.DMLOptions(); 
17	dml.DuplicateRuleHeader.AllowSave = true;
18	Database.SaveResult sr2 = Database.insert(duplicateAccount, dml);
19	if (sr2.isSuccess()) {
20		System.debug('Duplicate account has been inserted in Salesforce!');
21	}
22}

DuplicateError Methods

The following are methods for DuplicateError.

getDuplicateResult()

Returns the details of a duplicate rule and duplicate records found by the duplicate rule.

Signature

public Datacloud.DuplicateResult getDuplicateResult()

Return Value

Type: Datacloud.DuplicateResult

Example

This example shows the code used to get the possible duplicates and related match information after saving a new contact. This code is part of a custom application that implements duplicate management when users add a contact. See DuplicateResult Class to check out the entire sample applicaton.

1Datacloud.DuplicateResult duplicateResult = 
2                            duplicateError.getDuplicateResult();

getFields()

Returns an array of one or more field names. Identifies which fields in the object, if any, affected the error condition.

Signature

public List<String> getFields()

Return Value

Type: List<String>

getMessage()

Returns the error message text.

Signature

public String getMessage()

Return Value

Type: String

getStatusCode()

Returns a code that characterizes the error.

Signature

public StatusCode getStatusCode()

Return Value

Type: StatusCode