Check Duplicate EmailId when user Insert the Contact

Check Duplicate EmailId when user Insert the Contact


trigger OnContactChekDuplicateEmail on Contact (before insert) {
Set<String> emails = new Set<String>();
List<Contact>lstCon = new List<Contact>();
set<String> setEmail = new set<String>();

for(Contact Con:trigger.new){
emails.add(con.Email);
}
lstCon=[Select name,id, email from Contact where Email in:emails];

for(Contact con :lstCon){
 setEmail.add(con.email);
}

for(Contact c :trigger.new){ 
if(setEmail.contains(c.email)){
  c.adderror('Email duplicate');
}   
}
}

Comments

Popular posts from this blog

Salesforce Spring 16 Release Exam (Maintenance Exam Q&A) for Developer 201 Admin

Show Hyper Link On Add Error in Salesforce Trigger

Show the Success Message before Redirecting the Detail page on Visualforce Page