Skip to content

Instantly share code, notes, and snippets.

@goravseth
Created October 25, 2017 13:16
Show Gist options
  • Save goravseth/c847864982cc4923efed2b074ffe7cfa to your computer and use it in GitHub Desktop.
Save goravseth/c847864982cc4923efed2b074ffe7cfa to your computer and use it in GitHub Desktop.
invocable apex to find duplicate contacts using the new findDuplicates apex stuff
public class Flow_FindDupes {
//https://developer.salesforce.com/forums/?id=906F0000000AzDeIAK
@InvocableMethod(label='Get Dupes' description='Returns something')
public static List<Contact> getDuplicateContacts(List<Contact> contacts) {
system.debug('contacts size = ' + contacts.size());
list<sObject> dasContacts = new list<sobject>();
dasContacts.addall((List<sObject>)(contacts));
list<Contact> duplicateContacts = new list<Contact>();
Datacloud.FindDuplicatesResult[] results = Datacloud.FindDuplicates.findDuplicates(dasContacts);
if(results.size() > 0){
for (Datacloud.FindDuplicatesResult findDupeResult : results) {
for (Datacloud.DuplicateResult dupeResult : findDupeResult.getDuplicateResults()) {
for (Datacloud.MatchResult matchResult : dupeResult.getMatchResults()) {
for (Datacloud.MatchRecord matchRecord : matchResult.getMatchRecords()) {
System.debug('Duplicate Record: ' + matchRecord.getRecord());
contact c = (Contact) matchRecord.getRecord();
duplicateContacts.add(c);
}
}
}
}
}
return duplicateContacts;
}
}
@goravseth
Copy link
Author

very cool - i do recall hacking on this some years back! re the test class, if you enable duplicate rules on contact that would block the insert of those two identical contacts, i think your test class would fail. if your dupe rule is set to alert only and not to block, then you can use database.insert and set allowDuplicates to true - see this post : https://salesforce.stackexchange.com/questions/78670/disable-duplicate-check-for-apex-class

if its set to block, i'm not sure how to do a good test of it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment