You need to sign in to do that
Don't have an account?

How to check the contact duplication while editing??
Hi,
Using the below given trigger i am able to prevent user from being creating a contact with an existing Email Address, but i also need to check this while changing an existing contact's email address... i mean even for an existing contacts if the user try to change an email address with an existing email address.. the trigger should get fired??
trigger ContactDuplicateTrigger on Contact (before insert) {
for (Contact c : Trigger.new){
Contact[] contacts= [select id from Contact where Email = :c.Email];
if (contacts.size() > 0) {
c.Email.addError('Contact cannot be created - Contact already exists');
}
}
}
You may run into problems using this mechanism with bulk changes (e.g. inserts from data loader), as you have embedded a SOQL query inside a loop which may break governor limits. You'd do better to query against all emali addresses and then figure out afterwards which are the duplicates if you expect to receive bulk inserts/updates.
It looks like you'll need to make your trigger work before insert and before update in order to achieve this.
Something like the following I reckon - note that the code that I have added is also not bulk safe:
All Answers
You may run into problems using this mechanism with bulk changes (e.g. inserts from data loader), as you have embedded a SOQL query inside a loop which may break governor limits. You'd do better to query against all emali addresses and then figure out afterwards which are the duplicates if you expect to receive bulk inserts/updates.
It looks like you'll need to make your trigger work before insert and before update in order to achieve this.
Something like the following I reckon - note that the code that I have added is also not bulk safe:
Many Thanks.. it worked...
Bulk friendly version:
Unfortunately these lines:
will cause problems for before insert triggers - the sobjects contained in Trigger.new don't have ids (which is also why the Trigger.newMap is unavailable for before insert). In this case the records would need to be handled via another key - most likely email address.
I also think there's some confusion here - this version of the trigger appears to be checking that a contact with the same id doesn't already exist - the original use case was to check that no contact with the same email address exists.
Ugh, how embarassing. I really shouldn't post something when I'm trying to rush out the door.
Would this work? Or would the initial select break limits as well?
It stands a chance of breaking governor limits if there are more than 50k contacts.
I'd be inclined to create a map of the new/updated contacts keyed by email address (contByEmailMap), then pull back a list of all contacts that have that email address from the database. Then iterate the contacts returned from the database and get the matching contact(s) from contByEmailMap based on the email address.
There's a couple more gotchas though - contByEmailMap needs its values to be lists of contacts (as in bulk mode there may be more than one with the same email) and it would also need to check that there are no duplicates in the contacts being processed by the trigger - i.e. no dupes in the database but the same email appearing twice in trigger.new.
This is why I didn't put up any bulk safe code - I've implemented this once before and it gets a little unwieldy.