1

I'm writing some code to search for duplicates for a list of contacts. I need to be able to map the duplicates to the corresponding original/master contact. I haven't been able to find any documentation for that in the Datacloud docs. All the sample code I see assumes that there is only one contact in the list.

My code (con ids have been changed):

List<Id> conIds = new List<Id>{'00332QQQ026x7IX', '0036WQQ00od74b'};
Map<Id, Contact> dupeCons = new Map<Id, Contact>();
for(Datacloud.FindDuplicatesResult fdr: Datacloud.FindDuplicatesByIds.findDuplicatesByIds(conIds)){
        for(Datacloud.DuplicateResult dr: fdr.duplicateresults){
            for(Datacloud.MatchRecord m: dr.matchResults[0].getMatchRecords()){
                    Sobject obj = m.getRecord();
                    if(VF.getObjectNameById(obj.Id) == 'Contact'){
                        dupeCons.put(obj.Id,(Contact)obj);
                    }
            }
        }
}

I'd really like to have a Map so that the first key would be the original contact id, and each one would have a map of duplicates associated with it.

Something like:

Map<Id, Map<Id, Contact>

1 Answer 1

0

The number of duplicate results returned is the same number as the ids provided. The results will always return in the same order as the first id list. You can use the index of any duplicate to match against the id list.

I personally hate doing this, since somewhere else in the docs they mention that you can't rely on the order of a list being the same between runs, but this (and email send results) seem exempt from this rule. I've been running some code based on dupes and emails which is based on this principle for a few months now, and I haven't noticed any problems with it.

From Comments (@Jelle Van Genus)

when checking for duplicates of leads, the statement "The number of duplicate results returned is the same number as the ids provided" is not always true. Specifically, converted leads are ignored, leading to a mismatch in array sizes. So make sure to exclude converted leads when calling findDuplicates or findDuplicatesByIds.

2
  • I also hate doing this, but I just may anyway :) Thanks.
    – hgolov
    Jun 5, 2018 at 18:18
  • 1
    Small side note for an issue which caused me some grief: when checking for duplicates of leads, the statement "The number of duplicate results returned is the same number as the ids provided" is not always true. Specifically, converted leads are ignored, leading to a mismatch in array sizes. So make sure to exclude converted leads when calling findDuplicates or findDuplicatesByIds. Apr 19, 2019 at 6:34

You must log in to answer this question.

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