function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Terry411Terry411 

Database.DuplicateError not consistently identifying matching duplicates

I'm having an issue with Database.DuplicateError not consistently identifying matching duplicates. Has anyone else ran into this issue?

The code below is a snippet from a webservice that is inserting a single account record.
Database.UpsertResult ur = Database.upsert(a, false);

        boolean foundDupAccount = false;
        if (ur.isSuccess()){
            System.debug('---------- Successfully created Account. AccountID: ' + ur.getId() + ' ----------');                    
            a.Id = ur.getId();
        } else {
            system.debug('---------- FAILED to create Account ----------');
            for (Database.Error error : ur.getErrors()) {
                // Process only duplicates and not other errors (e.g., validation errors)
                if (error instanceof Database.DuplicateError) {
                    String failedDML = error.getMessage();

                    // Handle the duplicate error by first casting it as a DuplicateError class
                    Database.DuplicateError duplicateError = (Database.DuplicateError)error;
                    Datacloud.DuplicateResult duplicateResult = duplicateError.getDuplicateResult();

                    // Display duplicate error message as defined in the duplicate rule
                    // system.debug('Duplicate Error: ' + duplicateResult.getErrorMessage());
                    
                    // Get duplicate records
                    // Return only match results of matching rules that find duplicate records
                    Datacloud.MatchResult[] matchResults = duplicateResult.getMatchResults();

                    // Just grab first match result (which contains the duplicate record found and other match info)
                    Datacloud.MatchResult matchResult = matchResults[0];
                    Datacloud.MatchRecord[] matchRecords = matchResult.getMatchRecords();

                    // Add matched record to the duplicate records variable
                    for (Datacloud.MatchRecord matchRecord : matchRecords) {
                        System.debug('MatchRecord: ' + matchRecord.getRecord());
                        ID ExistingAccountID = matchRecord.getRecord().Id;
                        if( ExistingAccountID <> null){
                            System.debug('---------- Using Account Record ID: ' + ExistingAccountID + ' ----------');
                            a.Id = matchRecord.getRecord().Id;
                            foundDupAccount = true;
                            break;
                        }
                    }
                } else {
                    System.debug('---------- The following error has occurred.---------- ');                    
                    System.debug(error.getStatusCode() + ': ' + error.getMessage());
                    System.debug('---------- Account fields that affected this error: ' + error.getFields() + ' ---------- ');
                }
            }
        }

 
Alain CabonAlain Cabon

Already verified probably: the record-level security

User-added image

 
David Zhu 🔥David Zhu 🔥
I think there could be multiple matching rules. When insertion happens, the execution of matching rule is not deterministic.

Just curious, why not check duplication before insertion?

Datacloud.FindDuplicates.findDuplicates(sObjects);
for (Datacloud.FindDuplicatesResult findDupeResult : results)  {
                for (Datacloud.DuplicateResult dupeResult : findDupeResult.getDuplicateResults()) {
                    string rule = dupeResult.getDuplicateRule();
                    for (Datacloud.MatchResult matchResult : dupeResult.getMatchResults()) {
                        if (matchResult.getMatchRecords().size() > 0) {
                            //duplication found....