Skip to content

Instantly share code, notes, and snippets.

@jkentjnr
Last active August 16, 2022 11:42
Show Gist options
  • Save jkentjnr/b12ab751afa2e8603cddff20b1b24449 to your computer and use it in GitHub Desktop.
Save jkentjnr/b12ab751afa2e8603cddff20b1b24449 to your computer and use it in GitHub Desktop.
Duplicate Detection via Apex
Rule Name: MoveData Contact Duplicate
Rule Order: 3 of 3
Object: Contact
Record-Level Security: Enforce sharing rules
Action On Create: Allow
Operations On Create: Alert / Report
Action On Edit: Allow
Operations On Edit: Alert / Report
Alert Text: Use one of these records?
Active; Checked
Matching Rule: MoveData NPSP Contact Personal Email
Matching Criteria:
------------------
(Contact: FirstName EXACT MatchBlank = FALSE) AND
(Contact: LastName EXACT MatchBlank = FALSE) AND
(
(Contact: HomeEmail EXACT MatchBlank = FALSE) OR
(Contact: Email EXACT MatchBlank = FALSE) OR
(Contact: AlternateEmail EXACT MatchBlank = FALSE) OR
(Contact: WorkEmail EXACT MatchBlank = FALSE)
)
Conditions:
-----------
None
Contact record = new Contact();
record.FirstName = 'James';
record.LastName = 'Gilray';
record.Email = '[email protected]';
List<SObject> lst = new List<SObject>{ record };
Datacloud.FindDuplicatesResult[] results;
List<Id> duplicateIdList = new List<Id>();
try {
results = Datacloud.FindDuplicates.findDuplicates(lst);
}
catch (System.HandledException ex) {
System.Debug('System.HandledException: ' + ex);
//Continue regardless.
}
if(results != null && results.size() > 0){
for (Datacloud.FindDuplicatesResult findDupeResult : results) {
System.Debug('findDupeResult: ' + findDupeResult);
for (Datacloud.DuplicateResult dupeResult : findDupeResult.getDuplicateResults()) {
System.Debug('dupeResult: ' + dupeResult);
for (Datacloud.MatchResult matchResult : dupeResult.getMatchResults()) {
System.Debug('matchResult: ' + matchResult);
for (Datacloud.MatchRecord matchRecord : matchResult.getMatchRecords()) {
System.Debug('matchRecord: ' + matchRecord);
Id objectId = (Id)matchRecord.getRecord().get('Id');
duplicateIdList.add(objectId);
}
}
}
}
}
System.Debug('duplicateIdList');
System.Debug(duplicateIdList.size());
System.Debug(duplicateIdList);
55.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;NBA,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WAVE,INFO;WORKFLOW,INFO
Execute Anonymous: Contact record = new Contact();
Execute Anonymous: record.FirstName = 'James';
Execute Anonymous: record.LastName = 'Gilray';
Execute Anonymous: record.Email = '[email protected]';
Execute Anonymous:
Execute Anonymous: List<SObject> lst = new List<SObject>{ record };
Execute Anonymous:
Execute Anonymous: Datacloud.FindDuplicatesResult[] results;
Execute Anonymous: List<Id> duplicateIdList = new List<Id>();
Execute Anonymous:
Execute Anonymous: try {
Execute Anonymous: results = Datacloud.FindDuplicates.findDuplicates(lst);
Execute Anonymous: }
Execute Anonymous: catch (System.HandledException ex) {
Execute Anonymous: System.Debug('System.HandledException: ' + ex);
Execute Anonymous:
Execute Anonymous: //Continue regardless.
Execute Anonymous: }
Execute Anonymous:
Execute Anonymous: if(results != null && results.size() > 0){
Execute Anonymous: for (Datacloud.FindDuplicatesResult findDupeResult : results) {
Execute Anonymous: System.Debug('findDupeResult: ' + findDupeResult);
Execute Anonymous: for (Datacloud.DuplicateResult dupeResult : findDupeResult.getDuplicateResults()) {
Execute Anonymous: System.Debug('dupeResult: ' + dupeResult);
Execute Anonymous: for (Datacloud.MatchResult matchResult : dupeResult.getMatchResults()) {
Execute Anonymous: System.Debug('matchResult: ' + matchResult);
Execute Anonymous: for (Datacloud.MatchRecord matchRecord : matchResult.getMatchRecords()) {
Execute Anonymous: System.Debug('matchRecord: ' + matchRecord);
Execute Anonymous: Id objectId = (Id)matchRecord.getRecord().get('Id');
Execute Anonymous: duplicateIdList.add(objectId);
Execute Anonymous: }
Execute Anonymous: }
Execute Anonymous: }
Execute Anonymous: }
Execute Anonymous: }
Execute Anonymous:
Execute Anonymous: System.Debug('duplicateIdList');
Execute Anonymous: System.Debug(duplicateIdList.size());
Execute Anonymous: System.Debug(duplicateIdList);
20:52:08.1 (1411046)|USER_INFO|[EXTERNAL]|0055g00000DpsDs|[email protected]|(GMT+10:00) Australian Eastern Standard Time (Australia/Melbourne)|GMT+10:00
20:52:08.1 (1452704)|EXECUTION_STARTED
20:52:08.1 (1458050)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
20:52:08.1 (122779779)|USER_DEBUG|[22]|DEBUG|findDupeResult: Datacloud.FindDuplicatesResult[getDuplicateResults=(Datacloud.DuplicateResult[getDuplicateRule=NPSP_Contact_Personal_Email_Match;getDuplicateRuleEntityType=Contact;getErrorMessage=null;getMatchResults=(Datacloud.MatchResult[getEntityType=Contact;getErrors=();getMatchEngine=FuzzyMatchEngine;getMatchRecords=();getRule=NPSP_Contact_Personal_Email_Match;getSize=0;isSuccess=true;]);isAllowSave=false;], Datacloud.DuplicateResult[getDuplicateRule=Standard_Rule_for_Contacts_with_Duplicate_Leads;getDuplicateRuleEntityType=Contact;getErrorMessage=null;getMatchResults=(Datacloud.MatchResult[getEntityType=Lead;getErrors=();getMatchEngine=FuzzyMatchEngine;getMatchRecords=();getRule=Standard_Lead_Match_Rule_v1_0;getSize=0;isSuccess=true;]);isAllowSave=false;], Datacloud.DuplicateResult[getDuplicateRule=MoveData_Contact_Duplicate_Rule;getDuplicateRuleEntityType=Contact;getErrorMessage=null;getMatchResults=(Datacloud.MatchResult[getEntityType=Contact;getErrors=();getMatchEngine=ExactMatchEngine;getMatchRecords=();getRule=MoveData_NPSP_Contact_Personal_Email_Match;getSize=0;isSuccess=true;]);isAllowSave=false;]);getErrors=();isSuccess=true;]
20:52:08.1 (123463249)|USER_DEBUG|[24]|DEBUG|dupeResult: Datacloud.DuplicateResult[getDuplicateRule=NPSP_Contact_Personal_Email_Match;getDuplicateRuleEntityType=Contact;getErrorMessage=null;getMatchResults=(Datacloud.MatchResult[getEntityType=Contact;getErrors=();getMatchEngine=FuzzyMatchEngine;getMatchRecords=();getRule=NPSP_Contact_Personal_Email_Match;getSize=0;isSuccess=true;]);isAllowSave=false;]
20:52:08.1 (124065100)|USER_DEBUG|[26]|DEBUG|matchResult: Datacloud.MatchResult[getEntityType=Contact;getErrors=();getMatchEngine=FuzzyMatchEngine;getMatchRecords=();getRule=NPSP_Contact_Personal_Email_Match;getSize=0;isSuccess=true;]
20:52:08.1 (124806750)|USER_DEBUG|[24]|DEBUG|dupeResult: Datacloud.DuplicateResult[getDuplicateRule=Standard_Rule_for_Contacts_with_Duplicate_Leads;getDuplicateRuleEntityType=Contact;getErrorMessage=null;getMatchResults=(Datacloud.MatchResult[getEntityType=Lead;getErrors=();getMatchEngine=FuzzyMatchEngine;getMatchRecords=();getRule=Standard_Lead_Match_Rule_v1_0;getSize=0;isSuccess=true;]);isAllowSave=false;]
20:52:08.1 (125143924)|USER_DEBUG|[26]|DEBUG|matchResult: Datacloud.MatchResult[getEntityType=Lead;getErrors=();getMatchEngine=FuzzyMatchEngine;getMatchRecords=();getRule=Standard_Lead_Match_Rule_v1_0;getSize=0;isSuccess=true;]
20:52:08.1 (125663230)|USER_DEBUG|[24]|DEBUG|dupeResult: Datacloud.DuplicateResult[getDuplicateRule=MoveData_Contact_Duplicate_Rule;getDuplicateRuleEntityType=Contact;getErrorMessage=null;getMatchResults=(Datacloud.MatchResult[getEntityType=Contact;getErrors=();getMatchEngine=ExactMatchEngine;getMatchRecords=();getRule=MoveData_NPSP_Contact_Personal_Email_Match;getSize=0;isSuccess=true;]);isAllowSave=false;]
20:52:08.1 (126064225)|USER_DEBUG|[26]|DEBUG|matchResult: Datacloud.MatchResult[getEntityType=Contact;getErrors=();getMatchEngine=ExactMatchEngine;getMatchRecords=();getRule=MoveData_NPSP_Contact_Personal_Email_Match;getSize=0;isSuccess=true;]
20:52:08.1 (126550329)|USER_DEBUG|[37]|DEBUG|duplicateIdList
20:52:08.1 (126607714)|USER_DEBUG|[38]|DEBUG|0
20:52:08.1 (126651733)|USER_DEBUG|[39]|DEBUG|()
20:52:08.1 (126867922)|CODE_UNIT_FINISHED|execute_anonymous_apex
20:52:08.1 (126882197)|EXECUTION_FINISHED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment