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
LWC DeveloperLWC Developer 

I got this error during updating my contact due to trigger

I am trying to prevent duplicate those contacts whose email and phone are same on insert  and update events.
I got this error: 

PreventDuplicateInContact: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.PreventDuplicateInContact: line 29, column 1

Please look at my code and let me know where I did wrong.
Here is my trigger code: 
trigger PreventDuplicateInContact on Contact (before insert, before update) {
    Map<String, Contact> contactMap = new Map<String, Contact>();
    for (Contact newContact :Trigger.new) {
        
        // Make sure we don't treat an email address that  
        // isn't changing during an update as a duplicate.  
        
        if ((newContact.Email != null && newContact.Phone != null) &&  (Trigger.isInsert || (newContact.Email != Trigger.oldMap.get(newContact.Id).Email))) {
            if (contactMap.containsKey(newContact.Email) && contactMap.containsKey(newContact.Phone)) {
                newContact.Email.addError('Another Contact has the '  + 'same email address.');
            } else {
                contactMap.put(newContact.Email, newContact);
            }
        }
        
        if ((newContact.Phone != null) &&  (Trigger.isInsert || newContact.Phone != Trigger.oldMap.get(newContact.Id).Phone)) {
            if (contactMap.containsKey(newContact.Phone)) {
                newContact.Phone.addError('Another 	Contact has the '  + 'same Phone.');
            } else {
                contactMap.put(newContact.Phone, newContact);
            }
        }
    }
    List<Contact> oldContactList = [SELECT Email,Phone FROM Contact WHERE Email IN :contactMap.KeySet() OR Phone IN :contactMap.KeySet()];
    for (Contact oldContact : oldContactList) {
        Contact newContactEM = contactMap.get(oldContact.Email);
        
        Contact newContactPH = contactMap.get(oldContact.Phone);
        newContactPH.Phone.addError('Contact with this Phone '+ 'address already exists.');
    } 
    
}
Thank You!!
 
Best Answer chosen by LWC Developer
David Zhu 🔥David Zhu 🔥
Your code looks very clear., It looks good. Your put contact with changed email or phone  in contactMap. But it errors out at addError.
         
You may add a check to handle variable newcontatPh is null. It will fix your error.
Contact newContactEM = contactMap.get(oldContact.Email);
        
        Contact newContactPH = contactMap.get(oldContact.Phone);
        If (nrwContactPH !=null) {
              newContactPH.Phone.addError('Contact with this Phone '+ 'address already exists.');
        }

 

All Answers

David Zhu 🔥David Zhu 🔥
Your code looks very clear., It looks good. Your put contact with changed email or phone  in contactMap. But it errors out at addError.
         
You may add a check to handle variable newcontatPh is null. It will fix your error.
Contact newContactEM = contactMap.get(oldContact.Email);
        
        Contact newContactPH = contactMap.get(oldContact.Phone);
        If (nrwContactPH !=null) {
              newContactPH.Phone.addError('Contact with this Phone '+ 'address already exists.');
        }

 
This was selected as the best answer
LWC DeveloperLWC Developer
Thank you David. It is working properly!!