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 

Contact Dupe Preventer - User Receives Error - Any Ideas?

What is it about line 27 that would give the user creating the Contact this error, as well as email me the error?

 

Error: 


Apex script unhandled trigger exception by user/organization: 00560000000rvkR/00D6000000077GT

 

contactDuplicatePreventer: execution of BeforeUpdate

 

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

 

Trigger.contactDuplicatePreventer: line 27, column 1

 

Trigger:

 

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);
          System.debug('new contact = ' + newContact);
          System.debug('email = ' + newContact.Email);
        newContact.Email.addError('A Contact with this email address already exists.');}
        }
}

 

Thanks!

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
trigger contactDuplicatePreventer on Contact (before insert, before update) 
{
    Map<String, Contact> contactMap = new Map<String, Contact>();
	
	List<String> emailList = new List<String>();
	Set<String> existingEmailList = new Set<String>();

	If(Trigger.isBefore)
	{
		for(Integer i = 0; i < Trigger.new.size(); i++)
		{
			emailList.add(Trigger.new[i].email);
		}
		
		for(Contact c : [SELECT Id, Email FROM Contact WHERE Email IN :emailList])
		{
			existingEmailList.add(c.email);
		}

		if(Trigger.isInsert)
		{
			if(existingEmailList.size() > 0)
			{
				for(Integer i = 0; i < trigger.new.size(); i++)
				{
					if(existingEmailList.contains(Trigger.new[i].email))
					{
						Trigger.new[i].addError('Email Id already exists');
					}
				}
			}
		}
		
		if(Trigger.isUpdate)
		{
			if(existingEmailList.size() > 0)
			{
				for( Integer i = 0; i < trigger.new.size(); i++)
				{
					if(Trigger.new[i].Email != Trigger.old[i].Email)
					{
						if(existingEmailList.contains(Trigger.new[i].email))
						{
							Trigger.new[i].addError('Email Id already exists');
						}
					}
				}
			}
		}
	}	
}

 Try this

All Answers

iSqFt_ADiSqFt_AD

UPDATE:

 

So it appears, after talking to the rep, that the issue was she was attempting to edit an existing Contact and give them an email address that is already in Salesforce. 


Knowing this, how do I edit the trigger so that this example is covered by an error message, rather than a developer script exception?

Naidu PothiniNaidu Pothini
trigger contactDuplicatePreventer on Contact (before insert, before update) 
{
    Map<String, Contact> contactMap = new Map<String, Contact>();
	
	List<String> emailList = new List<String>();
	Set<String> existingEmailList = new Set<String>();

	If(isBefore)
	{
		for(Integer i = 0; i < Trigger.new.size(); i++)
		{
			emailList.add(Trigger.new[i].email);
		}
		
		for(Contact c : [SELECT Id, Email FROM Contact WHERE Email IN :emailList])
		{
			existingEmailList.add(c.email);
		}

		if(isInsert)
		{
			if(existingEmailList.size() > 0)
			{
				for( i = 0; i < trigger.new.size(); i++)
				{
					if(existingEmailList.contains(Trigger.new[i].email))
					{
						Trigger.new[i].addError('Email Id already exists');
					}
				}
			}
		}
		
		if(isUpdate)
		{
			if(existingEmailList.size() > 0)
			{
				for( i = 0; i < trigger.new.size(); i++)
				{
					if(Trigger.new[i].Email != Trigger.old[i].Email)
					{
						if(existingEmailList.contains(Trigger.new[i].email))
						{
							Trigger.new[i].addError('Email Id already exists');
						}
					}
				}
			}
		}
	}	
}	

 

Try this.

iSqFt_ADiSqFt_AD

What is "Integer"? I put this in my sandbox and got this error:

 

Error: Compile Error: Variable does not exist: i at line 24 column 22

 

Is there a way to just edit my existing trigger to get it to push an error message rather than the exception?

 

Thanks for your help.

Naidu PothiniNaidu Pothini

use integer insted of Integer

iSqFt_ADiSqFt_AD

Same error. What is "integer" representing?

Naidu PothiniNaidu Pothini
trigger contactDuplicatePreventer on Contact (before insert, before update) 
{
    Map<String, Contact> contactMap = new Map<String, Contact>();
	
	List<String> emailList = new List<String>();
	Set<String> existingEmailList = new Set<String>();

	If(isBefore)
	{
		for(Integer i = 0; i < Trigger.new.size(); i++)
		{
			emailList.add(Trigger.new[i].email);
		}
		
		for(Contact c : [SELECT Id, Email FROM Contact WHERE Email IN :emailList])
		{
			existingEmailList.add(c.email);
		}

		if(isInsert)
		{
			if(existingEmailList.size() > 0)
			{
				for(Integer i = 0; i < trigger.new.size(); i++)
				{
					if(existingEmailList.contains(Trigger.new[i].email))
					{
						Trigger.new[i].addError('Email Id already exists');
					}
				}
			}
		}
		
		if(isUpdate)
		{
			if(existingEmailList.size() > 0)
			{
				for( Integer i = 0; i < trigger.new.size(); i++)
				{
					if(Trigger.new[i].Email != Trigger.old[i].Email)
					{
						if(existingEmailList.contains(Trigger.new[i].email))
						{
							Trigger.new[i].addError('Email Id already exists');
						}
					}
				}
			}
		}
	}	
}	

 

Try this. I am initializing 'i'

iSqFt_ADiSqFt_AD

Error: Compile Error: Variable does not exist: isInsert at line 20 column 12

Naidu PothiniNaidu Pothini
trigger contactDuplicatePreventer on Contact (before insert, before update) 
{
    Map<String, Contact> contactMap = new Map<String, Contact>();
	
	List<String> emailList = new List<String>();
	Set<String> existingEmailList = new Set<String>();

	If(Trigger.isBefore)
	{
		for(Integer i = 0; i < Trigger.new.size(); i++)
		{
			emailList.add(Trigger.new[i].email);
		}
		
		for(Contact c : [SELECT Id, Email FROM Contact WHERE Email IN :emailList])
		{
			existingEmailList.add(c.email);
		}

		if(Trigger.isInsert)
		{
			if(existingEmailList.size() > 0)
			{
				for(Integer i = 0; i < trigger.new.size(); i++)
				{
					if(existingEmailList.contains(Trigger.new[i].email))
					{
						Trigger.new[i].addError('Email Id already exists');
					}
				}
			}
		}
		
		if(Trigger.isUpdate)
		{
			if(existingEmailList.size() > 0)
			{
				for( Integer i = 0; i < trigger.new.size(); i++)
				{
					if(Trigger.new[i].Email != Trigger.old[i].Email)
					{
						if(existingEmailList.contains(Trigger.new[i].email))
						{
							Trigger.new[i].addError('Email Id already exists');
						}
					}
				}
			}
		}
	}	
}

 Try this

This was selected as the best answer
iSqFt_ADiSqFt_AD

Thank you kindly!

iSqFt_ADiSqFt_AD

Any idea why I received almost 50 of these messages in the last 12 hours from this trigger? Thanks!

 

Apex script unhandled trigger exception by user/organization: 00560000001L3AU/00D6000000077GT

 

contactDuplicatePreventer: execution of BeforeUpdate

 

caused by: System.QueryException: Non-selective query against large object type (more than 100000 rows). Consider an indexed filter or contact salesforce.com about custom indexing.

Even if a field is indexed a filter might still not be selective when:

1. The filter value includes null (for instance binding with a list that contains null) 2. Data skew exists whereby the number of matching rows is very large (for instance, filtering for a particular foreign key value that occurs many times)

 

Trigger.contactDuplicatePreventer: line 15, column 1