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
WikWik 

Trigger - Help

Hi,

Need to write a trigger such that if a Contact already exists ( based on email, phone or name) on the Parent Account or any of the Parents' Child accounts, then if a user tries to Create a Contact, an error should be thrown.

This trigger should be Account based and not Org. 

Thank You.

Pritam ShekhawatPritam Shekhawat
hii wik ......
i give you a sample code for check duplicate value in trigger .....its useful for you if you find any issue then let me know .....
trigger StagesDuplicateM1 on Stages__c (before insert, before update) {
    Map<String, Stages__c> StagesMap = new Map<String, Stages__c>();
     for (Stages__c Stages : System.Trigger.new)
      {
        if ((Stages.M1__c != null) &&
        (System.Trigger.isInsert ||(Stages.M1__c !=System.Trigger.oldMap.get(Stages.Id).M1__c))) {
     
                  StagesMap.put(Stages.M1__c, Stages);           
        }
       
         if ((Stages.M2__c != null) &&
         (System.Trigger.isInsert ||(Stages.M2__c !=System.Trigger.oldMap.get(Stages.Id).M2__c))) {
     
                  StagesMap.put(Stages.M2__c, Stages);     
        
        }
 if ((Stages.M3__c != null) &&
 (System.Trigger.isInsert ||(Stages.M3__c !=System.Trigger.oldMap.get(Stages.Id).M3__c))) {
     
                  StagesMap.put(Stages.M3__c, Stages);     
      
  }
  }  
  for (Stages__c Stages : [SELECT M1__c FROM Stages__c WHERE M1__c IN :StagesMap.KeySet()])
      {
          Stages__c newStages = StagesMap.get(Stages.M1__c);
          newStages.M1__c.addError(Stages.M1__c + ' is ssaved here');
      }
  for (Stages__c Stages : [SELECT M2__c FROM Stages__c WHERE M2__c IN :StagesMap.KeySet()])
      {
          Stages__c newStages = StagesMap.get(Stages.M2__c);
          newStages.M2__c.addError(Stages.M2__c + ' is saved here');
      }    
      
  for (Stages__c Stages : [SELECT M3__c FROM Stages__c WHERE M3__c IN :StagesMap.KeySet()])
      {
          Stages__c newStages = StagesMap.get(Stages.M3__c);
          newStages.M3__c.addError(Stages.M3__c + ' is saved here ');
      }    
}
WikWik
what modifications can i bring to the following trigger to meet the requirement:

trigger PreventDuplicateContact on Contact (before insert,before update) {
    
    if(trigger.isInsert || trigger.isUpdate)
    {
        Map<Id,List<Contact>> accMap = new Map<Id,List<Contact>>();
        List<Contact> contactList;
        
        for(Contact c: [Select Id,Email,Phone,accountId from Contact]){
            if(accMap.containsKey(c.accountId)){   
                accMap.get(c.accountId).add(c);
            }
            else{
                contactList = new List<Contact>();
                contactList.add(c);
                accMap.put(c.accountId,contactList);
            }       
        }
        
        for (Contact c : Trigger.new) 
        {
            if(accMap.containsKey(c.accountId)){
                for(Contact con : accMap.get(c.accountId)){
                    if((c.Email == con.Email  || c.Phone == con.Phone) && (c.id!= con.id ) ){
                       // c.Email.addError('Contact with this email address already exists.Please create a Contact Role. <a href=https://cs18.salesforce.com/02Z/e?parentId='+c.accountid+'\'>Create a Contact Role</a>',false);
                       String baseUrl = URL.getSalesforceBaseUrl().toExternalForm()+'/02Z/e?parentId=';
string errorMsg = '<a style=\'color:1B2BE8\'href="'+baseUrl+ c.accountid +'"> Please Create a Contact Role  </a>';

c.addError('This Contact already exists.'+ errorMsg,false);
                    }
                }
            }
        }
    }
}
Vatsal KothariVatsal Kothari
Hi Wik,

You can refer below code:
trigger PreventDuplicateContactonAccount on Contact (before insert,before update) {
    
    if(trigger.isInsert || trigger.isUpdate)
    {
        Map<String, Contact> emailMap = new Map<String, Contact>();
        Map<Id,List<Contact>> accMap = new Map<Id,List<Contact>>();
        Set<Id> accIds = new Set<Id>();
        
        for(Contact con : trigger.new){
            if(con.AccountId != null){
                accIds.add(con.AccountId);
            }
        }
        
        for(Contact c: [Select Id,Email,accountId from Contact where AccountId IN: accIds]){
            if(accMap.containsKey(c.accountId)){   
                accMap.get(c.accountId).add(c);
            }
            else{
                List<Contact> contactList = new List<Contact>();
                contactList.add(c);
                accMap.put(c.accountId,contactList);
            }       
        }
        
        for (Contact c : Trigger.new)
        {
            if(accMap.containsKey(c.accountId)){
                for(Contact con : accMap.get(c.accountId)){
                    if(c.Email == con.Email){
                        c.Email.addError('Contact with this email address already exists');
                    }
                }
            }
        }       
    }
}
Above code will check Account based duplicate contact based on Email.

If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
WikWik
this acts on a single account level and doesnot take into consideration the Parent Child thing as i am looking for:

"Need to write a trigger such that if a Contact already exists ( based on email, phone or name) on the Parent Account or any of the Parents' Child accounts, then if a user tries to Create a Contact, an error should be thrown.

This trigger should be Account based and not Org. "
Vatsal KothariVatsal Kothari
Above trigger is based on Account level only, If duplicate contact is created on same account than only it will give an error.

If you try to create duplicate contact on different account than it will not give you any error.
WikWik
Yup. But the thing i am looking for is:
Lets say a Contact is created on Account 'B'. Account 'A' is the parent for account B. and if i try to create same contact on Account 'C' ( another Child of Account 'A'), error should be thrown.
WikWik
or also if the Contact exists on Parent and if we try to create  a duplicate on any of its Child, even then an error should be thrown.