You need to sign in to do that
Don't have an account?
m 10
Error: Compile Error: Variable does not exist: con2 at line 36 column 13
need to write mulitple error messages below trigger for multiple fields
Note:suppose if enter name it is throwing error as duplicate for next i am entering email also error message need to display as duplicate same as like phone also
trigger AvoidDuplicate on Contact (before insert)
{
set<string> newNameSet = new set<string>();
set<string> newEmailSet = new set<string>();
set<string> newPhoneSet = new set<string>();
set<string> dbNameSet = new set<string>();
set<string> dbEmailSet = new set<string>();
set<string> dbPhoneSet = new set<string>();
for(Contact con : trigger.new){
newNameSet.add(con.LastName);
newEmailSet.add(con.Email);
newPhoneSet.add(con.Phone);
}
List<contact> bcon = [select id, LastName,Phone, email from contact where email IN: newEmailSet OR LastName IN: newNameSet OR Phone IN: newPhoneSet];
for(Contact dbcon:Trigger.new)
{
dbNameSet.add(dbcon.LastName);
dbEmailSet.add(dbcon.Email);
dbPhoneSet.add(dbcon.Phone);
}
for(contact con1 : trigger.new){
if(dbNameSet.contains(con1.LastName) && dbEmailSet.Contains(con1.Email) && dbPhoneSet.Contains(con1.Phone))
{
con1.addError('You are inserting Duplicate LastName');
con2.addError('You are inserting Duplicate Email');
con3.addError('You are inserting Duplicate Phone');
}
}
}
Note:suppose if enter name it is throwing error as duplicate for next i am entering email also error message need to display as duplicate same as like phone also
trigger AvoidDuplicate on Contact (before insert)
{
set<string> newNameSet = new set<string>();
set<string> newEmailSet = new set<string>();
set<string> newPhoneSet = new set<string>();
set<string> dbNameSet = new set<string>();
set<string> dbEmailSet = new set<string>();
set<string> dbPhoneSet = new set<string>();
for(Contact con : trigger.new){
newNameSet.add(con.LastName);
newEmailSet.add(con.Email);
newPhoneSet.add(con.Phone);
}
List<contact> bcon = [select id, LastName,Phone, email from contact where email IN: newEmailSet OR LastName IN: newNameSet OR Phone IN: newPhoneSet];
for(Contact dbcon:Trigger.new)
{
dbNameSet.add(dbcon.LastName);
dbEmailSet.add(dbcon.Email);
dbPhoneSet.add(dbcon.Phone);
}
for(contact con1 : trigger.new){
if(dbNameSet.contains(con1.LastName) && dbEmailSet.Contains(con1.Email) && dbPhoneSet.Contains(con1.Phone))
{
con1.addError('You are inserting Duplicate LastName');
con2.addError('You are inserting Duplicate Email');
con3.addError('You are inserting Duplicate Phone');
}
}
}
Used below code it will help you:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
thanks ,could you please one more scenario if cutomer contact is available need to display 'yes' are if it is not there need to diplay 'no' and create a new contact.
thanks imran
Now it working fine:
Thanks,
Ajay Dubedi