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

Urgent #### Trigger to check duplicate for 3 fields on object
Hello folks ,
Requirement: Can any one tell me how to write a trigger on contact to check the duplicates for Name , phone , email . if any duplicate arises we need to put error message . condition is (Name OR phone OR Email).
trigger DuplicateTwoFieldsContact on Contact (before insert,before update)
{
if(trigger.isBefore)
{
map<String,Contact> mapcontact = new map<String,Contact>();
map<String,Contact> phonemap = new map<String,Contact>();
for(contact con: trigger.new)
{
if(
((con.Email != null) && (Trigger.isInsert || con.email != trigger.oldmap.get(con.id).email )) ||
((con.Phone != null) && (Trigger.isInsert || con.Phone != trigger.oldmap.get(con.id).Phone ))
)
{
if( !mapcontact.containskey(con.Email) || !phonemap.containskey(con.Phone))
{
mapcontact.put(con.Email ,con);
phonemap.put(con.Phone ,con);
}
}
}
for(contact c:[select id ,Email ,Phone from contact where Email =: mapcontact.keyset() OR Phone =: phonemap.keyset() ])
{
contact newcontact = mapcontact.get(c.Email);
newcontact.Email.adderror('Duplicate Email has been detected ');
contact Phonecontact = phonemap.get(c.Phone);
Phonecontact.Phone.adderror('Duplicate Phone has been detected ');
}
}
}
Requirement: Can any one tell me how to write a trigger on contact to check the duplicates for Name , phone , email . if any duplicate arises we need to put error message . condition is (Name OR phone OR Email).
trigger DuplicateTwoFieldsContact on Contact (before insert,before update)
{
if(trigger.isBefore)
{
map<String,Contact> mapcontact = new map<String,Contact>();
map<String,Contact> phonemap = new map<String,Contact>();
for(contact con: trigger.new)
{
if(
((con.Email != null) && (Trigger.isInsert || con.email != trigger.oldmap.get(con.id).email )) ||
((con.Phone != null) && (Trigger.isInsert || con.Phone != trigger.oldmap.get(con.id).Phone ))
)
{
if( !mapcontact.containskey(con.Email) || !phonemap.containskey(con.Phone))
{
mapcontact.put(con.Email ,con);
phonemap.put(con.Phone ,con);
}
}
}
for(contact c:[select id ,Email ,Phone from contact where Email =: mapcontact.keyset() OR Phone =: phonemap.keyset() ])
{
contact newcontact = mapcontact.get(c.Email);
newcontact.Email.adderror('Duplicate Email has been detected ');
contact Phonecontact = phonemap.get(c.Phone);
Phonecontact.Phone.adderror('Duplicate Phone has been detected ');
}
}
}
Try This :
Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.
Thanks
Varaprasad
@For Salesforce Project Support: varaprasad4sfdc@gmail.com
Salesforce latest interview questions :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1
All Answers
Try This :
Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.
Thanks
Varaprasad
@For Salesforce Project Support: varaprasad4sfdc@gmail.com
Salesforce latest interview questions :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1
Because you can do the same with validation rule as well, using vlookup.
Thanks
Thanks in Adv alll of you