Skip to content

Instantly share code, notes, and snippets.

@goravseth
Created October 25, 2017 13:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • 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

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!

@jakeiscrazy
Copy link

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);

 }
 
}

@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