+ Start a Discussion
Salesforce####Salesforce#### 

Urgent #### Trigger to check duplicate for 3 fields on object

Hello folks , 
Requirement:  Can any one tell me how to write a trigger on contact to check the duplicates for Name , phone , email . if any duplicate arises we need to put error message . condition is (Name OR phone OR Email).

trigger DuplicateTwoFieldsContact on Contact (before insert,before update) 
{
 if(trigger.isBefore)
 {
  map<String,Contact> mapcontact = new map<String,Contact>();
   map<String,Contact> phonemap = new map<String,Contact>();
  
  for(contact con: trigger.new) 
   {
   if(                                                 
   ((con.Email != null) && (Trigger.isInsert || con.email != trigger.oldmap.get(con.id).email )) ||
    ((con.Phone != null) && (Trigger.isInsert || con.Phone != trigger.oldmap.get(con.id).Phone ))
    )
     {                                
     if( !mapcontact.containskey(con.Email) || !phonemap.containskey(con.Phone))
      {
      mapcontact.put(con.Email ,con);
      phonemap.put(con.Phone ,con);
      
      } 
     }
   }
 
 for(contact c:[select id ,Email ,Phone from contact where Email =: mapcontact.keyset() OR Phone =: phonemap.keyset() ])
 {
 contact newcontact = mapcontact.get(c.Email);
  newcontact.Email.adderror('Duplicate Email has been detected ');
 
 contact Phonecontact = phonemap.get(c.Phone);
  Phonecontact.Phone.adderror('Duplicate Phone has been detected ');
 
 }
 }
}
Best Answer chosen by Salesforce####
v varaprasadv varaprasad
Hi,

Try This : 
 
trigger DuplicateTwoFieldsContact on Contact (before insert,before update) 
{
 if(trigger.isBefore){

  set<string> phones = new set<string>();
  set<string> emails = new set<string>();
  set<string> conNames =  ew set<string>();
  
  for(contact con: trigger.new){
      if(con.Email != null && con.phone != null){
	    emails.add(con.Email);
		phones.add(con.phone);
		conNames.add(con.name)
	  }
  
    } 
   
   list<contact> existingCOns = [select id,name,phone,email from contact where name in :conNames OR Email in: emails OR phone in : phones];
   
   if(existingCOns.size()>0){
     for(contact con: trigger.new){
	    con.addError('Name OR phone OR Email duplicate value exists');
	 }
   
   }
   
   
   
  }
}

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Salesforce Project Support: varaprasad4sfdc@gmail.com

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1

All Answers

v varaprasadv varaprasad
Hi,

Try This : 
 
trigger DuplicateTwoFieldsContact on Contact (before insert,before update) 
{
 if(trigger.isBefore){

  set<string> phones = new set<string>();
  set<string> emails = new set<string>();
  set<string> conNames =  ew set<string>();
  
  for(contact con: trigger.new){
      if(con.Email != null && con.phone != null){
	    emails.add(con.Email);
		phones.add(con.phone);
		conNames.add(con.name)
	  }
  
    } 
   
   list<contact> existingCOns = [select id,name,phone,email from contact where name in :conNames OR Email in: emails OR phone in : phones];
   
   if(existingCOns.size()>0){
     for(contact con: trigger.new){
	    con.addError('Name OR phone OR Email duplicate value exists');
	 }
   
   }
   
   
   
  }
}

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Salesforce Project Support: varaprasad4sfdc@gmail.com

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1
This was selected as the best answer
AshishkAshishk
Do you need the trigger only for this? Is it a part of requirment?

Because you can do the same with validation rule as well, using vlookup.

Thanks
Salesforce####Salesforce####
@AshishK : Can you tell me how can we do that with validation rule as well, using vlookup.   which is actually a good idea

Thanks in Adv alll of you 
AshishkAshishk
Check this :- https://success.salesforce.com/answers?id=90630000000gyFzAAI