+ Start a Discussion
@ M  Coder@ M Coder 

Duplication trigger on contact at account level - please modify my code

can anyone please modify my code . 

Under an account , there should be only one contact with name phone . if i create one more contact with same name then the error shoulp appear . my code :
trigger PreventDuplicateContactMap 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,LastName,phone,Email,accountId from Contact where AccountId !=Null ]){
            if(accMap.containsKey(c.accountId)){   
                accMap.get(c.accountId).add(c);
                  system.debug('If loop ===');
            }
            else{
            system.debug('else loop ===');
                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.Phone== con.Phone) || ( c.LastName == con.LastName ) )
                    {
                        c.addError('Duplicate Contact.......... ');
                    }
                }
            }
        }
    }
}
Best Answer chosen by @ M Coder
Rohit Kumar SainiRohit Kumar Saini
Hi,

I updated your code. Below should work. It will prevent two contacts for a single Account having same last name or same Phone number.
 
trigger PreventDuplicateContactMap on Contact (before insert,before update) {    
    if(trigger.isInsert || trigger.isUpdate)
    {
        List<Id> accountIds=new List<Id>();
        //first collect all related Accounts for inserted or updated contacts
        for(Contact c: Trigger.New){
            accountIds.add(c.accountId);
        }
        // Then query all contacts related to these accounts and create map having all contacts associated to each Account
        Map<Id,List<Contact>> accMap = new Map<Id,List<Contact>>();
        for(Contact c: [Select Id,LastName,phone,Email,accountId from Contact where AccountId in :accountIds]){
            if(accMap.containsKey(c.accountId)){   
                List<Contact> existingContactsForThisAccount=accMap.get(c.accountId);
                existingContactsForThisAccount.add(c);
                accMap.put(c.accountId, existingContactsForThisAccount);
            }
            else{
                accMap.put(c.accountId, new List<Contact>{c});
            }       
        }
        
        //Real logic starts here        
        for (Contact c : Trigger.new) 
        {
            if(accMap.containsKey(c.accountId))
            {
                for(Contact con : accMap.get(c.accountId))
                {
                    if( (c.Phone== con.Phone) || ( c.LastName == con.LastName ) )
                    {
                        c.addError('Duplicate Contact.......... ');
                    }
                }
            }
        }
    }
}

Please let me know for any issues or questions. Thanks.

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi,

Below is the sample code for duplicate emails, you can change accordingly.
​​​​​​​
trigger PreventDuplicateContact on Contact (before insert,before update) {
    if(trigger.isInsert || trigger.isUpdate)
    {
        Map<String, Contact> emailMap = new Map<String, Contact>();
        
        for (Contact c : System.Trigger.new) 
        {
            if ((c.Email != null) && (System.Trigger.isInsert || (c.Email != System.Trigger.oldMap.get(c.Id).Email))) {
                if(emailMap.containsKey(c.Email)) {
                    c.Email.addError('Contact has the same Email address');
                }
                else {
                    emailMap.put(c.Email, c);
                }
            }            
            
        }
    
        for (Contact c : [SELECT Email FROM Contact WHERE Email IN :emailMap.KeySet()]) {
            Contact c1 = emailMap.get(c.Email);            
            c1.Email.addError('Contact with this email address already exists');
        }
    
    }
   
}


   Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
                                             
Best Regards
Sandhya
 
 
 
 
Rohit Kumar SainiRohit Kumar Saini
Hi,

I updated your code. Below should work. It will prevent two contacts for a single Account having same last name or same Phone number.
 
trigger PreventDuplicateContactMap on Contact (before insert,before update) {    
    if(trigger.isInsert || trigger.isUpdate)
    {
        List<Id> accountIds=new List<Id>();
        //first collect all related Accounts for inserted or updated contacts
        for(Contact c: Trigger.New){
            accountIds.add(c.accountId);
        }
        // Then query all contacts related to these accounts and create map having all contacts associated to each Account
        Map<Id,List<Contact>> accMap = new Map<Id,List<Contact>>();
        for(Contact c: [Select Id,LastName,phone,Email,accountId from Contact where AccountId in :accountIds]){
            if(accMap.containsKey(c.accountId)){   
                List<Contact> existingContactsForThisAccount=accMap.get(c.accountId);
                existingContactsForThisAccount.add(c);
                accMap.put(c.accountId, existingContactsForThisAccount);
            }
            else{
                accMap.put(c.accountId, new List<Contact>{c});
            }       
        }
        
        //Real logic starts here        
        for (Contact c : Trigger.new) 
        {
            if(accMap.containsKey(c.accountId))
            {
                for(Contact con : accMap.get(c.accountId))
                {
                    if( (c.Phone== con.Phone) || ( c.LastName == con.LastName ) )
                    {
                        c.addError('Duplicate Contact.......... ');
                    }
                }
            }
        }
    }
}

Please let me know for any issues or questions. Thanks.
This was selected as the best answer