-
-
Save goravseth/c847864982cc4923efed2b074ffe7cfa to your computer and use it in GitHub Desktop.
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 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.
@isTest
public class TestFlow_FindDupes {
@isTest public static void insertRecord(){
List<Contact> lstContact = new List<Contact>{
new Contact(LastName = 'Mike', FirstName= 'Mike', Email='john_doe@us.ibm.com', Phone='(804) 441-1681', LeadSource='referral'),
new Contact(LastName = 'Mike', FirstName= 'Mike', Email='john_doe@us.ibm.com', Phone='(804) 441-1682', LeadSource='referral')};
insert lstContact;
System.debug('lstContact ' + lstContact);
list<Contact> testResponse = Flow_FindContactDupes.getDuplicateContacts(lstContact);
System.debug('testResponse ' + testResponse);
integer testResponseSize = testResponse.size();
System.assertNotEquals(0, testResponseSize);
}
}
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!
uses the findDuplicates classes available in the winter18 release
this code is very basic. it will fail if there are multiple matches, and can only accept a single sObject variable from the flow. just a proof-o-concept!