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
iSqFt_ADiSqFt_AD 

Unknown Error - Need Trigger Update Assistance

trigger contactDuplicatePreventer on Contact (before insert, before update) {

    Map<String, Contact> contactMap = new Map<String, Contact>();

    for (Contact contact : System.Trigger.new) {
    
        // Make sure we don't treat an email address that
        // isn't changing during an update as a duplicate.
        if ((contact.Email != null) && (System.Trigger.isInsert || (contact.Email != System.Trigger.oldMap.get(contact.Id).Email))) {
    
            // Make sure another new contact isn't also a duplicate
            if (contactMap.containsKey(contact.Email)) {
                contact.Email.addError('Another new Contact has the same email address.');
            } else {
                contactMap.put(contact.Email, contact);
            }
        }
    }
    
    // Using a single database query, find all the contacts in
    // the database that have the same email address as any
    // of the contacts being inserted or updated.
for (Contact contact : [SELECT Email FROM Contact WHERE Email IN :contactMap.KeySet()]) { if(contact.email != null){
        Contact newContact = contactMap.get(contact.Email);
        newContact.Email.addError('A Contact with this email address already exists.'); }
    }
}

 

I found the above trigger online and have implemented it into our production instance of SFDC. It appears to work except I occasionally get emails with the following error:

 

"

Subject: Developer script exception from iSqFt : contactDuplicatePreventer : contactDuplicatePreventer: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.contactDuplicatePreventer: line 25, column 9

 

Apex script unhandled trigger exception by user/organization: 005600000019X1a/00D6000000077GT

 

contactDuplicatePreventer: execution of BeforeUpdate

 

caused by: System.NullPointerException: Attempt to de-reference a null object

 

Trigger.contactDuplicatePreventer: line 25, column 9"

 

Can anyone tell me what I need to do to fix the trigger to prevent this? The user is also seeing a similar error on screen which is quite confusing to them.


Thank you.

Damien_Damien_

Which is line 25?

iSqFt_ADiSqFt_AD
        newContact.Email.addError('A Contact with this email address already exists.'); }

 

Damien_Damien_

My guess is that the email was never filled out and so it is null.  The line will pretty much be translated to:

newContact.null.addError('A Contact with this email address already exists.'); }

 

In order to fix it you can make sure that you don't worry about null emails in the query.

 

for (Contact contact : [SELECT Email FROM Contact WHERE Email IN :contactMap.KeySet() AND Email != null])

Alternatively, you can force users to enter a value for the email with a validation rule.  Only use this though if that's what you truly want.  If you want to allow them to not enter an email use the above.

 

Hopefully this works/helps.

Damien_Damien_

Nevermind.... you check for null above.  This may not work.

iSqFt_ADiSqFt_AD

Thats what I thought. I am not a developer which is why I am reaching out as I cannot figure out why it is throwing this error. 

 

As much as I would love to require email, the users would riot in that they cannot always get an email address from people and it would cause them to either not put contacts in Salesforce at all, or put in bogus email addresses to suffice the rule.

Damien_Damien_

Change the lines to this and replicate the error after putting up a debug log.  See what the values of new contact and email are.  If either one are null it will give you problems, but I'm not sure why either should be null.

 

 

 

for (Contact contact : [SELECT Email FROM Contact WHERE Email IN :contactMap.KeySet()]) {
if(contact.email != null){
Contact newContact = contactMap.get(contact.Email);
System.debug('new contact = ' + newContact);
System.debug('email = ' + newContact.Email);
newContact.Email.addError('A Contact with this email address already exists.');
}
}

 

iSqFt_ADiSqFt_AD

I made the suggested changes and cannot get the error to duplicate. I did not know exactly what the user was doing to cause the error in the first place. At this point I will have to wait and see if I get any more errors similar to this.

Thanks for your help! 

craigmhcraigmh

Change this

 

Contact newContact = contactMap.get(contact.Email);
newContact.Email.addError('A Contact with this email address already exists.');

 

To this.

 

Contact newContact = contactMap.get(contact.Email);
if(newContact != null) { newContact.Email.addError('A Contact with this email address already exists.'); }

 

I have a feeling that the Contact record being returned is null.

iSqFt_ADiSqFt_AD

Thanks Craig. I will hold onto this and update it if the previous suggestion continues to throw errors. I have not received any since the change but I will go days without seeing any error notifications. 

craigmhcraigmh

Yeah, it's one of those weird things when the data may not always match up with what you expect.

 

It's like when you do a Select query, and you think you should get one record back, but it still breaks. That's why I normally Select into a List, and check the size() method/property (should be a property, yet it has parentheses).

 

Anyways, when you do the Select query on Contact, that's a separate data set from your Map values, so it's definitely worth checking to see if the newContact variable isn't null.

Arish_KhanArish_Khan

Hi iSqFt_AD,

 

I think the code marked bold is throwing the error :

 

            if (contactMap.containsKey(contact.Email)) {
                contact.Email.addError('Another new Contact has the same email address.');
            } else {
                contactMap.put(contact.Email, contact);
            }

 

 

Try this

 

if (!contactMap.containsKey(contact.Email)) {

        contactMap.put(contact.Email, contact);

}

 

 

Let me know if it help,