3

We are trying to get duplicate records for Leads, but it looks like we need a DML operation in order to do so (the code I'm referring to is post 2 on this thread https://developer.salesforce.com/forums/?id=906F0000000BTlMIAW).

Sample Code:

Account a=new Account();
a.name='Burlington Textiles Corp of America';
Database.SaveResult res=DataBase.insert(a,false);
if(res.isSuccess()){
    // Operation was successful, so get the ID of the record that was 
    processed
    System.debug('Successfully inserted account. Account ID: ' + 
    res.getId());
}
else{   
    Datacloud.DuplicateResult duplicateResult;
    List<Datacloud.MatchRecord> matchRecords;
    List<Id> duplicateIds = new List<id>();
    // Operation failed, so get all errors                
    for(Database.Error duplicateError : res.getErrors()){
        duplicateResult = ((Database.DuplicateError)duplicateError).getDuplicateResult();
       System.debug('The following error has occurred.--duplicateResult--'+duplicateResult);                         
    }
    for(Datacloud.MatchResult duplicateMatchResult : duplicateResult.getMatchResults()){
        matchRecords = duplicateMatchResult.getMatchRecords();
        System.debug('The following error has occurred.--matchRecords--'+matchRecords);  
    }
    System.debug('total.--matchRecords--'+matchRecords);  
    for(Datacloud.MatchRecord duplicateMatchRecord : matchRecords){
        duplicateIds.add(duplicateMatchRecord.getRecord().Id);
        System.debug('duplicateIds--'+duplicateIds);
    }
    System.debug('Total duplicateIds--'+duplicateIds);
}

Salesforce provides the functionality through the SOAP API (https://developer.salesforce.com/forums/?id=906F0000000BTlMIAW), but the example they provide only provides the call out, but not how it's handled in APEX (https://developer.salesforce.com/forums/?id=906F0000000BTlMIAW).

So, does anyone know of a way to find duplicates identified by the OOTB Salesforce duplicate and matching rules in APEX without doing a DML operation?

3 Answers 3

3

If you want to find duplicates of new records, you need to use the method proposed here (i.e. you need a DML operation). This is useful only when the Duplicate Rule is set to Block. If you have an existing lead or leads that you want to examine, you can query DuplicateRecordSet and DuplicateRecordMember to find records that have already been matched as duplicates.


At a basic level, you can query for all of the matched duplicates, which will be grouped by Duplicate Rule, using a query like the following:

DuplicateRecordSet[] dupes = [
    SELECT (SELECT RecordId FROM DuplicateRecordItems) 
    FROM DuplicateRecordSet 
    WHERE Id IN (SELECT DuplicateRecordSetId
                 FROM DuplicateRecordItem 
                 WHERE RecordId = :leadIds)
];
4
  • sfdcfox, do you know of any examples in APEX that utilize DuplicateRecordSet? Commented May 23, 2017 at 15:14
  • @user3562811 SELECT (SELECT RecordId FROM DuplicateRecordItems) FROM DuplicateRecordSet WHERE Id IN (SELECT DuplicateRecordSetId FROM DuplicateRecordItem WHERE RecordId = :leadId)
    – sfdcfox
    Commented May 23, 2017 at 15:31
  • @Santanu Boral pointed me to your thread related to my question: salesforce.stackexchange.com/questions/175200/… . I am also trying to identify potential duplicates detected by the standard matching rule. It seems however that DuplicateRecordItems are created only on DML operations. Is it possible to use standard matching rules to identify potential duplicates on existing data? (without inserting or updating)
    – AlexandreJ
    Commented May 26, 2017 at 15:40
  • @AlexandreJ You would have to do your own queries. There's no API to determine duplicates beforehand, as far as I can tell.
    – sfdcfox
    Commented May 26, 2017 at 15:54
-1

Probably, this suggestion can be helpful:

SELECT name, company, COUNT(Id) FROM lead GROUP BY name, company HAVING COUNT(Id) > 1

-2

DupeCatcher application will do that for you and it is free DupeCatcher

5
  • We want to use Salesforce's out of the box duplicate management, so DupeCatcher is not an option. Thank you for pointing it out though. Commented May 23, 2017 at 15:01
  • Just doubting if a custom unique field which will be populated by workflow will work for you. Commented May 23, 2017 at 15:04
  • Avijit, can you please elaborate? Commented May 23, 2017 at 15:07
  • How are you deciding duplicate? for say email + Name must be unique so create a unique field and try to populate that field using workflow it will prevent the creation of the record. Not sure it will help if you need a robust and flexible code I will encourage DML & Apex. Commented May 23, 2017 at 15:12
  • 2
    Thanks for the idea Avijit. Unfortunately we need to use the out of box duplicate management rules with fuzzy matching Commented May 24, 2017 at 15:23

You must log in to answer this question.

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