• LWC Developer
  • NEWBIE
  • 10 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
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!!
 
Scenario 1: I want to display all records.of selected objects from drop down list.
Scenario 2: If I open any account after above scenario then I would like to display all the files of that Account.

For Example:
If I select Account object from Drop down list of sObjects then It would be display all records of Account  object and their associated files after clicking on any Account record.
Is that possible by Lightning Component? Please let me know the solution of above scenarios.
I have some urgent need of this requirement.

Thanks in advance...
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!!