I am getting a duplicate rule issue with apex

first error: DUPLICATES_DETECTED, Use one of these records?: []

Now I want this rule to run, however, I need to get the details of the record that is being suggested by Salesforce duplicate management. How can I get the record details that is being suggested in Apex and show it as part of error message?

Let me know for any further details.!!


1 Answer 1


You can use DuplicateResult class to get duplicate records detected by duplicate rule.

Using Database.SaveResult saveResult = Database.insert(contact, false); get errors and successes. You may need to do few tweaks in code to get duplicate records.

for (Database.Error error : saveResult.getErrors()) {
     if (error instanceof Database.DuplicateError) {
         Database.DuplicateError duplicateError = (Database.DuplicateError)error;
         Datacloud.DuplicateResult duplicateResult = duplicateError.getDuplicateResult();

         ApexPages.Message errorMessage = new ApexPages.Message(
                            ApexPages.Severity.ERROR, 'Duplicate Error: ' + 

         this.duplicateRecords = new List<sObject>();

         Datacloud.MatchResult[] matchResults = duplicateResult.getMatchResults();

         Datacloud.MatchResult matchResult = matchResults[0];
         Datacloud.MatchRecord[] matchRecords = matchResult.getMatchRecords();

You can use Datacloud.MatchResult to get duplicate records. Please find the sample code.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .