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

Trigger to avoid duplicate records
Hello,
I have followed the below code not to allow duplicates. But I don't want to display the error message. If there is duplicate record is created or existing record updated then shouldn't throw error simply skip that record. I mean do not insert the record in the object.
Can someone help me
I have followed the below code not to allow duplicates. But I don't want to display the error message. If there is duplicate record is created or existing record updated then shouldn't throw error simply skip that record. I mean do not insert the record in the object.
Can someone help me
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()]) { Contact newContact = contactMap.get(Contact.Email); newContact.Email.addError('A Contact with this email ' + 'address already exists.'); } }
For you remove the erro message comment theses lines:
Contact.Email.addError('Another new Contact has the ' + 'same email address.');
newContact.Email.addError('A Contact with this email ' + 'address already exists.');
The trigger is check if there are Contacts with the same e-mail address.
Best regrets,
Marcilio
Thanks for your reply.
In your code, in line 28 still the error message is displayed.
I have updated the code based on your inputs. I could still see the error message.
What I am looking is if user enter new contact with same email, then the new contact record shouldn't throw error message. Without throwing error message that record should be cleaned.