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
sriram k 15sriram k 15 

Requirement to Print Error message If you enter duplicate values in Email And LastName fields in Contact Record? Can someone help me Trigger is firing only for LastName field not for Email?

trigger duplication_Rules_Contact on Contact (before insert,before update,after undelete) {

    if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)){
        ContactTriggerHepler.duplicateRules(trigger.new);
    }
    if(trigger.isAfter && trigger.isUndelete){
        ContactTriggerHepler.duplicateRules(trigger.new);
    }
}

public class ContactTriggerHepler {

    
    public static void duplicateRules(List<Contact> licon){
        set<String> NewEmail = new set<String>();
        set<String> ExistingEmail = new Set<String>();
        set<String> NlastName = new set<String>();
        set<String> ExistingLastName = new set<String>();
        for(Contact con : licon){
            if(con.Email != null)  {
                NewEmail.add(con.Email);
            }
            if(con.LastName != null){
                NlastName.add(con.LastName);
            }
        }
        
        list<Contact> existingContact = [SELECT Id,Email,Phone,LastName from Contact where (Email =: NewEmail AND Email != null) AND
                                                                                  (LastName =: NlastName AND LastName != null) ];
        for(Contact co : existingContact){
            ExistingEmail.add(co.Email);
            ExistingLastName.add(co.LastName);
        }
        
        for(Contact  c : licon){
            if(ExistingEmail.contains(c.email)){
                c.addError('Duplicate Email in Contact');
            }
            else{
                ExistingEmail.add(c.Email);
            }
        }
        for(Contact  c : licon){
            if(ExistingLastName.contains(c.LastName)){
                c.addError('Duplicate LastName in Contact');
            }
            else{
                ExistingLastName.add(c.LastName);
            }
        }
    }
}
Sai PraveenSai Praveen (Salesforce Developers) 
Hi SriRam,

Can you change the condition from AND to OR in the query as shown below.
 
list<Contact> existingContact = [SELECT Id,Email,Phone,LastName from Contact where (Email =: NewEmail AND Email != null) OR
                                                                                  (LastName =: NlastName AND LastName != null) ];

If this solution helps, Please mark it as best answer.

Thanks,
 
CharuDuttCharuDutt
Hii Simran
Try Below Code
trigger PreventDuplicateContacts on Contact (before insert) {
    Set <String> emailSet = new Set<String>(); 
    Set <String> lastNameSet = new Set<String>(); 
    for (contact con:trigger.new) {
if(Con.Email != null){        
emailSet.add(con.Email);
}
if(Con.lastName!= null){        
lastNameSet.add(Con.lastName);
}
        
    }
    List <Contact> contactList = [SELECT Email,Phone FROM Contact WHERE email IN :emailSet And LastName In: lastNameSet];

    for (contact con:trigger.new) {
        If (contactList.size() > 0) {
            
          con.Email.adderror( 'Duplicate Contact Found. Use Existing Contact.' );
            
        }
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
sriram k 15sriram k 15

Hi CharuDutt,

The above code is giving error message with duplicate Email Field only. It is not giving error for duplicate LastName field.

ex: If we give  Email : Different value     (Doesn't exist in contact record's Email Field)

                       Lastname : Duplicate value  (Existing in other contact record's  LastName field)

It is not working.(I need both Email and LastName fields should be unique in each Record)