Created
October 25, 2017 13:16
-
-
Save goravseth/c847864982cc4923efed2b074ffe7cfa to your computer and use it in GitHub Desktop.
invocable apex to find duplicate contacts using the new findDuplicates apex stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
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
goravseth this was so helpful! I am a non dev admin but was able to modify this to work for my needs and create an advanced duplicate checker using flow that also uses the standard rules.
I also wrote this super basic test class to get it into prod.