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

I am trying to create a trigger on contact to prevent duplicate records based on Contact Email & Contact Phone.
I tried this code but not working. Can any one help me on this?
trigger Contactduplicaterecord on Contact (before insert, before update)
{
set<string> EmaPho=new set<string>();
for(Contact Con:trigger.new)
{
EmaPho.add(Con.Phone);
EmaPho.add(Con.Email);
}
if(EmaPho.size()>0 )
{
List<Contact> lstContact=[Select Phone,Email from Contact where Phone in:EmaPho or Email in:EmaPho];
Map<string, Contact> MapEmaPhowisecontact=new Map<string, Contact>();
for(Contact Con:lstContact)
{
MapEmaPhowisecontact.put(Con.Phone, Con);
MapEmaPhowisecontact.put(Con.Email, Con);
}
for(Contact Con:trigger.new)
{
if(MapEmaPhowisecontact.containsKey(Con.Phone))
{
Con.Phone.addError('Phone Number already Exist');
}
if(MapEmaPhowisecontact.containsKey(Con.Email))
{
Con.Email.addError('Email already Exist');
}
}
}
}
trigger Contactduplicaterecord on Contact (before insert, before update)
{
set<string> EmaPho=new set<string>();
for(Contact Con:trigger.new)
{
EmaPho.add(Con.Phone);
EmaPho.add(Con.Email);
}
if(EmaPho.size()>0 )
{
List<Contact> lstContact=[Select Phone,Email from Contact where Phone in:EmaPho or Email in:EmaPho];
Map<string, Contact> MapEmaPhowisecontact=new Map<string, Contact>();
for(Contact Con:lstContact)
{
MapEmaPhowisecontact.put(Con.Phone, Con);
MapEmaPhowisecontact.put(Con.Email, Con);
}
for(Contact Con:trigger.new)
{
if(MapEmaPhowisecontact.containsKey(Con.Phone))
{
Con.Phone.addError('Phone Number already Exist');
}
if(MapEmaPhowisecontact.containsKey(Con.Email))
{
Con.Email.addError('Email already Exist');
}
}
}
}
I have update your same code... there is minor changes and it's working fine my dev org...
Thanks,
Maharajan.C
All Answers
Please try with below code.
If this helps, please mark it as best answer.
Regards,
Ankaiah
Try Below trigger Please Mark It As Best Answer If It Helps
Thank You!
Hii Salluri.
Please try with below code.
Trigger:-
Handler:-
If you find the above solution helpful, then please mark it as Best Answer to help others too.
Thanks and Regards,
Suraj.
I have update your same code... there is minor changes and it's working fine my dev org...
Thanks,
Maharajan.C
Please use below code:-
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
I have updated your same code... there are minor changes and it's working fine on my dev org...
Trigger Contactduplicaterecord on Contact (before insert, before update) {
if (Trigger.isbefore && (Trigger.isInsert || Trigger.isUpdate)) {
Set<String> setEmail = new Set<String>();
Set<String> setPhone = new Set<String>();
for (Contact con : Trigger.new) {
if (con.Email != null) {
setEmail.add(con.Email);
}
if (con.Phone != null) {
setPhone.add(con.Phone);
}
}
List<Contact> existingContacts = [SELECT Id, Phone, Email FROM Contact WHERE Email IN :setEmail OR Phone IN :setPhone];
Map<String, Contact> contactMap = new Map<String, Contact>();
for (Contact con : existingContacts) {
if (con.Email != null) {
contactMap.put(con.Email, con);
}
if (con.Phone != null) {
contactMap.put(con.Phone, con);
}
}
for (Contact con : Trigger.new) {
if (con.Phone != null && contactMap.containsKey(con.Phone)) {
con.Phone.addError('Phone number already exists on another contact.');
}
if (con.Email != null && contactMap.containsKey(con.Email)) {
con.Email.addError('Email already exists on another contact.');
}
}
}
}
if you need any assistance, please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Anand