Created
October 31, 2020 13:57
-
-
Save sachi-d/8c1711bf4aad3c9ffaf14513059ceadf to your computer and use it in GitHub Desktop.
If you want to check for duplicates before trying to insert, that too can be done using the FindDuplicates class in Datacloud namespace.
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
Class__c classToCreate = new Class__c(Name='Science', Grade__c=8); | |
List<Class__c> duplicates = findDuplicateRecords(classToCreate); | |
if(duplicates.size() == 0){ | |
//no duplicates | |
insert classToCreate; | |
} else{ | |
//desired update | |
} | |
public static List<SObject> findDuplicateRecords(Class__c classToCreate){ | |
List<Class__c> classList = new List<Class__c>(); | |
classList.add(classToCreate); | |
List<Datacloud.FindDuplicatesResult > findDuplicatesResult = Datacloud.FindDuplicates.findDuplicates(classList); | |
Datacloud.DuplicateResult duplicateResult = findDuplicatesResult.get(0).getDuplicateResults().get(0); | |
Datacloud.MatchResult matchResult = duplicateResult.getMatchResults()[0]; | |
List<Datacloud.MatchRecord> matchRecords = matchResult.getMatchRecords(); | |
List<SObject> duplicates = new List<SObject>(); | |
for(Datacloud.MatchRecord matchRecord : matchRecords){ | |
SObject duplicate = matchRecord.getRecord(); | |
System.debug('Match Record: ' + duplicate); | |
duplicates.add(duplicate); | |
} | |
return duplicates; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment