You need to sign in to do that
Don't have an account?
Procurement Executive
How to update a contact if it caught as duplicate in trigger ?
Can anyone help me with the duplicate contact usdate via trigger.
Code:-
trigger ContactDuplicateTrigger on Contact (before insert) {
//private final Contact contact;
// Initialize a list to hold any duplicate records
private List<sObject> duplicateRecords;
// Define variable that’s true if there are duplicate records
public boolean hasDuplicateResult{get;set;}
Contact cont = new Contact( FirstName= 'Robin', LastName='Singh',Email='pintub.sfdc+123@gmail.com' );
Database.SaveResult saveResult = Database.insert(cont, 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();
ApexPages.Message errorMessage = new ApexPages.Message(
ApexPages.Severity.ERROR, 'Duplicate Error: ' +
duplicateResult.getErrorMessage());
ApexPages.addMessage(errorMessage);
this.duplicateRecords = new List<sObject>();
Datacloud.MatchResult[] matchResults = duplicateResult.getMatchResults();
Datacloud.MatchResult matchResult = matchResults[0];
Datacloud.MatchRecord[] matchRecords = matchResult.getMatchRecords();
this.hasDuplicateResult = !this.duplicateRecords.isEmpty();
}
}
}
}
Code:-
trigger ContactDuplicateTrigger on Contact (before insert) {
//private final Contact contact;
// Initialize a list to hold any duplicate records
private List<sObject> duplicateRecords;
// Define variable that’s true if there are duplicate records
public boolean hasDuplicateResult{get;set;}
Contact cont = new Contact( FirstName= 'Robin', LastName='Singh',Email='pintub.sfdc+123@gmail.com' );
Database.SaveResult saveResult = Database.insert(cont, 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();
ApexPages.Message errorMessage = new ApexPages.Message(
ApexPages.Severity.ERROR, 'Duplicate Error: ' +
duplicateResult.getErrorMessage());
ApexPages.addMessage(errorMessage);
this.duplicateRecords = new List<sObject>();
Datacloud.MatchResult[] matchResults = duplicateResult.getMatchResults();
Datacloud.MatchResult matchResult = matchResults[0];
Datacloud.MatchRecord[] matchRecords = matchResult.getMatchRecords();
this.hasDuplicateResult = !this.duplicateRecords.isEmpty();
}
}
}
}
You can include condition so exclude invoking trigger for one profile so that it trigger won't be invoked.
Check below link to bypass trigger validation rules
http://www.infallibletechie.com/2018/10/how-to-bypass-all-triggers-validation.html
Also, you can check inbuild functionality 'Duplicate Management rules' which stops the creation of duplicate contacts.
Hope above information was helpful.
Please mark as Best Answer so that it can help others in the future.
Thanks,
Vinay Kumar
Also, review below reference link.
https://developer.salesforce.com/forums/?id=9062I000000g4sGQAQ
Hope above information was helpful.
Thanks,
Vinay Kumar