function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Procurement ExecutiveProcurement 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();
     }
}
}
}
VinayVinay (Salesforce Developers) 
Hi,

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
Procurement ExecutiveProcurement Executive
Thank you for replying, As I am new to salesforce, it would be helpful if I gets code snippets or example.
VinayVinay (Salesforce Developers) 
Review below sample snippet.
 
trigger Acc on Account (before delete)

    {
  
Profile userProfile = [SELECT Id FROM Profile WHERE Name='Sales User']; 
if(userProfile.Id = UserInfo.getProfileId()) 
​return;
 
     if (Trigger.isDelete) {

           for (Account ac : Trigger.old)

      {

             ac.addError('Cannot delete');

      }

     }}

Also, review below reference link.

https://developer.salesforce.com/forums/?id=9062I000000g4sGQAQ

Hope above information was helpful.

Thanks,
Vinay Kumar