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
m 10m 10 

Error: Compile Error: Variable does not exist: con2 at line 36 column 13

need to write mulitple error messages below trigger for multiple fields
Note:suppose if enter name it is throwing error as duplicate for next i am entering email also error message need to display as duplicate same as like phone also

trigger AvoidDuplicate on Contact (before insert)
{    
    
    set<string> newNameSet = new set<string>();
    
    set<string> newEmailSet = new set<string>();
    set<string> newPhoneSet = new set<string>();
    set<string> dbNameSet = new set<string>();
    
    set<string> dbEmailSet = new set<string>();
    set<string> dbPhoneSet = new set<string>();
    
    for(Contact con : trigger.new){
        
        newNameSet.add(con.LastName);
        
        newEmailSet.add(con.Email);
        newPhoneSet.add(con.Phone);
        
    }
    
    List<contact> bcon = [select id, LastName,Phone, email from contact where email IN: newEmailSet OR LastName IN: newNameSet OR Phone IN: newPhoneSet];
   for(Contact dbcon:Trigger.new)
    {
        
        dbNameSet.add(dbcon.LastName);
        
        dbEmailSet.add(dbcon.Email);
        dbPhoneSet.add(dbcon.Phone);
    }
    
    for(contact con1 : trigger.new){        
        if(dbNameSet.contains(con1.LastName) && dbEmailSet.Contains(con1.Email) && dbPhoneSet.Contains(con1.Phone))
        {
            con1.addError('You are inserting Duplicate LastName');
            con2.addError('You are inserting Duplicate Email');
            con3.addError('You are inserting Duplicate Phone');
        }
        
        
    }
    
}
Ajay K DubediAjay K Dubedi
Hi Imran,

Used below code it will help you:
 
trigger AvoidDuplicate on Contact (before insert){
    set<string> newNameSet = new set<string>();
    set<string> newEmailSet = new set<string>();
    set<string> newPhoneSet = new set<string>();
    set<string> dbNameSet = new set<string>();    
    set<string> dbEmailSet = new set<string>();
    set<string> dbPhoneSet = new set<string>();
    for(Contact con : trigger.new){
        newNameSet.add(con.LastName);
        if(con.Email != NULL && con.Phone != NULL){
            newEmailSet.add(con.Email);
            newPhoneSet.add(con.Phone);
        }
    }
    List<Contact> ContactList = new List<Contact>();
    ContactList = [SELECT id, LastName,Phone, Email FROM Contact where Email IN: newEmailSet OR LastName IN: newNameSet OR Phone IN: newPhoneSet];
    if(ContactList.size() > 0){
        for(Contact conObj: ContactList){
            newNameSet.add(conObj.LastName);
            if(conObj.Email != NULL && conObj.Phone != NULL){
                newEmailSet.add(conObj.Email);
                newPhoneSet.add(conObj.Phone);
            }
            
        }
    }
     for(contact con1 : trigger.new){        
        if(dbNameSet.contains(con1.LastName)){
            con1.addError('You are inserting Duplicate LastName');
            
        }
        else if(dbEmailSet.Contains(con1.Email)){
            con1.addError('You are inserting Duplicate Email');
        }
        else if(dbPhoneSet.Contains(con1.Phone)){
             con1.addError('You are inserting Duplicate Phone');
        }
        
        
    }
    
}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
m 10m 10
Hi Ajay,

thanks ,could you please one more scenario if cutomer contact is available need to display 'yes' are if it is not there need to diplay 'no' and create a new  contact.

thanks imran
m 10m 10
trigger is not firing culd you please check i t once
 
Ajay K DubediAjay K Dubedi
Hi Imran,

Now it working fine:
trigger AvoidDuplicate on Contact (before insert){
    System.debug('Hello');
    set<string> newNameSet = new set<string>();
    set<string> newEmailSet = new set<string>();
    set<string> newPhoneSet = new set<string>();
    set<string> dbNameSet = new set<string>();    
    set<string> dbEmailSet = new set<string>();
    set<string> dbPhoneSet = new set<string>();
    for(Contact con : trigger.new){
        System.debug('Hello1');
        newNameSet.add(con.LastName);
        if(con.Email != NULL && con.Phone != NULL){
            newEmailSet.add(con.Email);
            newPhoneSet.add(con.Phone);
        }
    }
    List<Contact> ContactList = new List<Contact>();
    ContactList = [SELECT id, LastName,Phone, Email FROM Contact where Email IN: newEmailSet OR LastName IN: newNameSet OR Phone IN: newPhoneSet];
    if(ContactList.size() > 0){
          System.debug('Hello2');
        for(Contact conObj: ContactList){
            dbNameSet.add(conObj.LastName);
            if(conObj.Email != NULL && conObj.Phone != NULL){
                dbEmailSet.add(conObj.Email);
                dbPhoneSet.add(conObj.Phone);
            }
            
        }
    }
     for(contact con1 : trigger.new){  
         System.debug('ContactList '+ContactList);
        if(dbNameSet.Contains(con1.LastName)){
            con1.addError('You are inserting Duplicate LastName');
            System.debug('Hello3');
        }
        else if(dbEmailSet.Contains(con1.Email)){
            con1.addError('You are inserting Duplicate Email');
        }
        else if(dbPhoneSet.Contains(con1.Phone)){
             con1.addError('You are inserting Duplicate Phone');
        }
        
        
    }
    
}

Thanks,
Ajay Dubedi