Created
October 31, 2020 13:56
-
-
Save sachi-d/64dbe25bd905fc87ccf50759398e3e45 to your computer and use it in GitHub Desktop.
The duplicate rule can be captured by the errors thrown at insertion as follows.
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); | |
Database.SaveResult saveResult = Database.insert(classToCreate, false); | |
if (!saveResult.isSuccess()) { | |
for (Database.Error error : saveResult.getErrors()) { | |
if (error instanceof Database.DuplicateError) { | |
Database.DuplicateError duplicateError = (Database.DuplicateError)error; | |
Datacloud.DuplicateResult duplicateResult = duplicateError.getDuplicateResult(); | |
System.debug(duplicateResult.getErrorMessage()); | |
Datacloud.MatchResult matchResult = duplicateResult.getMatchResults()[0]; | |
Datacloud.MatchRecord[] matchRecords = matchResult.getMatchRecords(); | |
for (Datacloud.MatchRecord matchRecord : matchRecords) { | |
System.debug('MatchRecord: ' + matchRecord.getRecord()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment