+ Start a Discussion
Sainath VenkatSainath Venkat 

trigger on specific record type for contact duplication

Can anyone help me to fire the below trigger on a specific record type only.
I have record type of "Business Organization (Mastered)" and below trigger should fire only on the mentioned record type.

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,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.Email.addError('Contact with this email address already exists');
                    }
                }
            }
        }
    }
}
Best Answer chosen by Sainath Venkat
Raj VakatiRaj Vakati
try this
 
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,accountId ,Account.Recordtype.Name ,from Contact where Account.Recordtype.Name='Business Organization (Mastered)']){
		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.Email.addError('Contact with this email address already exists');
				}
			}
		
		}
	}
}
}

 

All Answers

Raj VakatiRaj Vakati
Try this
 
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,accountId from Contact where Recordtype.Name='Business Organization (Mastered)']){
            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.Email.addError('Contact with this email address already exists');
                    }
                }
            }
        }
    }
}

 
Raj VakatiRaj Vakati

OR


 

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,accountId from Contact where Recordtype.Name='Business Organization (Mastered)']){
		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(c.Recordtype.Name=='Business Organization (Mastered)'){
		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');
				}
			}
		}
		}
	}
}
}
Sainath VenkatSainath Venkat
Hi Raj,
Actually, the record type that I have mentioned is present on ACCOUNT object but not on CONTACT object
 
Raj VakatiRaj Vakati
try this
 
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,accountId ,Account.Recordtype.Name ,from Contact where Account.Recordtype.Name='Business Organization (Mastered)']){
		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.Email.addError('Contact with this email address already exists');
				}
			}
		
		}
	}
}
}

 
This was selected as the best answer
Sainath VenkatSainath Venkat
Hi Raj,
Your trigger worked for me, I have written a test class previously, can you please help me with the modifications for the below test class


@isTest
private class PreventDuplicateContactTest {
 
    @isTest static void TestTrigger(){
     
        
        Account testAcct1 = new Account(Name='Test Account');
        insert testAcct1;
        
        Account testAcct2 = new Account(Name='Test Account');
        insert testAcct2;
        
        
        Contact cont = new Contact(FirstName = 'Test', LastName = 'LastName', Email='test@tst.com', accountid = testAcct2.id);
        insert cont;
        
        Contact cont2 = new Contact(FirstName = 'Test2', LastName = 'LastName', Email='testt@tst.com', accountid = testAcct1.id);
        insert cont2;
        
        List<Contact> contacts = new List<Contact>();
        for (Integer i = 1; i < 4; i++){
            contacts.add(new Contact(FirstName = 'Test'+i, LastName = 'LastName'+i, Email='test@tst.com', accountid = testAcct2.id));
        }
        
        try {
            insert contacts;
        } catch (Exception e) {
            System.assert(e.getMessage().contains('Contact with this email address already exists'));
        }   
    }
}
Gaurav Jain 7Gaurav Jain 7
Hi Sainath,

I am considering record type of "Business Organization (Mastered)" on Contact object.

Try Below code:

I have used to get contact record type info: 

Map<ID, Schema.RecordTypeInfo> rtMap = Schema.SObjectType.Contact.getRecordTypeInfosById();


To validate record type name used below code:

String type = rtMap.get(c.RecordTypeId).getName();
if(type.equals('Business_Organization_Mastered')){
trigger PreventDuplicateContact on Contact (before insert,before update) {
    
   Map<ID, Schema.RecordTypeInfo> rtMap = Schema.SObjectType.Contact.getRecordTypeInfosById();
   
    if(trigger.isInsert || trigger.isUpdate)
    {
        Map<Id,List<Contact>> accMap = new Map<Id,List<Contact>>();
        List<Contact> contactList;
        
        for(Contact c: [Select Id,Email,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)
        {
       String type = rtMap.get(c.RecordTypeId).getName();
            if(type.equals('Business_Organization_Mastered')){
            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');
                    }
                }
            }
           } 
        }
    }
}

Mark it as a best answer if it helps
Sainath VenkatSainath Venkat
Thanks for all your responses guys, I am able to complete the task and I got 100% code coverage also.