You need to sign in to do that
Don't have an account?
LWC 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:
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!!
All Answers