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
Nish321Nish321 

trigger to check for duplicate email and throw error

Not able to figure out what's wrong with this trigger.  
 
trigger DuplicateEmail on Contact (before insert, before update) {

map<string, contact> conmap =  new map<string, contact> ();

   for(contact con : trigger.new)
   
      {
        if(con.email!=null){
        
        conmap.put(con.email, con);
        
        }
      
     }
     
     
 map<string, contact> emailmap = new map<string, contact> ([select email, id from contact where email IN: conmap.keyset() limit 1]);    
 

 
      for(contact c : trigger.new)
      
      {
      
      
      
     if(emailmap.get(c.email) !=null)
      
      {
        c.email.adderror('this is duplicate email');
      }
    
   
   }
     
     
     
}

 
Best Answer chosen by Nish321
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below
trigger DuplicateEmail on Contact (before insert, before update) 
{
	Set<String> setEmailID = new set<String>();
	Set<Id> setContID = new set<ID>();
    for (Contact Contact : System.Trigger.new) 
	{
        if ((Contact.Email != null) &&  (System.Trigger.isInsert ||  (Contact.Email != System.Trigger.oldMap.get(Contact.Id).Email))) 
		{
			setEmailID.add(Contact.Email);
			setContID.add(Contact.id);
		}
    }

	List<Contact> lstCOntact = [select id ,email from contact where email in :setEmailID and id not in :setContID ];
    Map<String, Contact> contactMap = new Map<String, Contact>();

	for(Contact cont : lstCOntact)
	{
		contactMap.put(cont.email, cont);
	}	

    for (Contact Contact : System.Trigger.new) 
	{
        if ((Contact.Email != null) &&  (System.Trigger.isInsert ||  (Contact.Email != System.Trigger.oldMap.get(Contact.Id).Email))) 
		{
			if(contactMap.containsKey(Contact.Email))
			{
				Contact.Email.addError('A Contact with this email address already exists.');
			}
		}	
	}	
}

Please let us know if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below
trigger DuplicateEmail on Contact (before insert, before update) 
{
	Set<String> setEmailID = new set<String>();
	Set<Id> setContID = new set<ID>();
    for (Contact Contact : System.Trigger.new) 
	{
        if ((Contact.Email != null) &&  (System.Trigger.isInsert ||  (Contact.Email != System.Trigger.oldMap.get(Contact.Id).Email))) 
		{
			setEmailID.add(Contact.Email);
			setContID.add(Contact.id);
		}
    }

	List<Contact> lstCOntact = [select id ,email from contact where email in :setEmailID and id not in :setContID ];
    Map<String, Contact> contactMap = new Map<String, Contact>();

	for(Contact cont : lstCOntact)
	{
		contactMap.put(cont.email, cont);
	}	

    for (Contact Contact : System.Trigger.new) 
	{
        if ((Contact.Email != null) &&  (System.Trigger.isInsert ||  (Contact.Email != System.Trigger.oldMap.get(Contact.Id).Email))) 
		{
			if(contactMap.containsKey(Contact.Email))
			{
				Contact.Email.addError('A Contact with this email address already exists.');
			}
		}	
	}	
}

Please let us know if this will help you
 
This was selected as the best answer
Tarun J.Tarun J.
Hello Ramshah,

Your query at Line #16 will return map which has ID as key instead of Email. If you capture SOQL return data in Map, it will store ID as key and value as sObject. So, emailMap is actually havin Contact Id as key and not the email value. Hence your code at line #27 will not satisfy the criteria. You need to update the code to find a duplicate. There are multiple ways to re-write this code, I am just rewritting the query and duplicate check part of the code.
trigger DuplicateEmail on Contact (before insert, before update) {

	map<string, contact> conmap =  new map<string, contact> ();

   for(contact con : trigger.new)  
    {
        if(con.email!=null){
        
			conmap.put(con.email, con);
        
        }
      
    }          
	//Map<string, contact> emailmap = new Map<string, contact> ([select email, id from contact where email IN: conmap.keyset()]);     
 
	for(contact c : [SELECT Email, Id FROM Contact WHERE Email IN: conmap.keyset()])
	{
		if(conmap.containsKey(c.Email)){
			conmap.get(c.email).adderror('This is duplicate email');
		}      
    }     
}

-Thanks,
TK

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Nish321Nish321
Thanks Amit and Tarun.  Both your answers resolved this issue.  
@Tarun :  Thanks again for your detailed explanation to make me understand what was causing the issue.