You need to sign in to do that
Don't have an account?
suresh dupada
How to Check Duplicate email in the trigger while insert and update
How to write trigger to avoid duplicate email when bulkfying the trigger
my trigger is:
trigger AvoidDuplicate on contact (before insert,before update)
{
if(Trigger.isInsert||Trigger.isUpdate)
for(contact a:trigger.new)
{
integer count=[select count from contact where email=:a.email];
if(count>0)
{
a.Email.adderror('This email already exists');
}
}
}
this is working fine when inserting single records
but it shows error when working with dataloader...... Please help to to over come this error
I would appriciate for any kind of replay...............
my trigger is:
trigger AvoidDuplicate on contact (before insert,before update)
{
if(Trigger.isInsert||Trigger.isUpdate)
for(contact a:trigger.new)
{
integer count=[select count from contact where email=:a.email];
if(count>0)
{
a.Email.adderror('This email already exists');
}
}
}
this is working fine when inserting single records
but it shows error when working with dataloader...... Please help to to over come this error
I would appriciate for any kind of replay...............
Try below code
Check the below link for your reference
http://developer.force.com/cookbook/recipe/preventing-duplicate-records-from-saving
Regards,
Bhanu Mahesh
All Answers
Try below code
Check the below link for your reference
http://developer.force.com/cookbook/recipe/preventing-duplicate-records-from-saving
Regards,
Bhanu Mahesh
First of all your code is not bulkfied it will throw Too Many SOQL error if bulk uploading happen,
Here is the correct code.
Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
I used your code, but found a bug while finding duplicates record on base of Email in the imported batch, i.e. If imported batch have duplicate email itself it doesn't handle that.
Here is an updated code for the same:
trigger DuplicateEmailsInContacts on Contact (before insert, before update){
Set<String> EmailSet = new set<String>();
Set<String> ExistingEmailSet = new set<string>();
for(Contact c : Trigger.New){
IF(c.Email != '' && c.Email != NULL){
If(!EmailSet.contains(c.Email)){
EmailSet.add(c.Email);
}
}
}
//Updating ExistingEmailSet
for(Contact con : [SELECT Email From Contact where Email IN :EmailSet]){
ExistingEmailSet.add(con.Email);
}
IF(Trigger.IsInsert || Trigger.IsUpdate){
for(Contact c1 : Trigger.New){
System.Debug(ExistingEmailSet);
IF(ExistingEmailSet.contains(c1.Email)){
c1.Email.addError('Email Already Exists : '+c1.Email);
}else if(c1.Email != null || c1.Email != ''){
ExistingEmailSet.add(c1.Email);
}
}
}
}
List<String> lstEmail = new List<String>();
for(contact con:trigger.new){
lstEmail.add(con.Email);
}
List<Contact> lstContact=[select id from contact where email IN :lstEmail];
if(lstContact.size() > 0){
con.Email.adderror('This email already exists');
}
}
Suppose i have alredy existing User contact with Email address xyz@gmail.com which is active
If i try to create the 2 nd User with same email address xyz@gmail.com it will through error that email already exists because the first user is active.
I want to make my trigger to work like if xyz@gmail.com(1st user) is inactive for first user it should allow me to use the same email address for creating the second user with xyz@gmail.com.
* Name : DuplicateContactRecordCheck using Set
* @author: Akash Khairnar
Handler Class For Trigger
****************************************/
public with sharing class DuplicateContactRecordCheck {
public static void NoDuplicateRecord(List<Contact> myContactlist)
{
Set<String> myEmail =new Set<String> (); //Initializing Set for Email
Set<String> myphone= new set<String>(); //Initializing Set For Phone
List<Contact> fetchList=[SELECT Email ,Phone FROM Contact]; //Query to retrive all contacts
for(Contact c:fetchList) //traverse the list
{
myEmail.add(c.Email); //Adding Email record to set
myphone.add(c.Phone); //Adding Phone reord to set
}
for(Contact c : myContactlist) //Traversing through trigger List
{
if(c.Email != null && myEmail.contains(c.Email) || (c.Phone != null && myphone.contains(c.Phone))) //condition to check duplicate
{
c.Email.AddError(' Duplicate Email is not Allowed '); //Error Message on Email
c.Phone.AddError(' Duplicate Phone is not Allowed '); //Error Messahe on Phone
} // end of If
}//end of for loop
}// end of Method
}//end of class
// Trigger For Handler Class
trigger DuplicateContacts on Contact (before insert) {
DuplicateContactRecordChecl.NoDuplicateRecord(Trigger.new);
}