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

Prevent Duplicate Contact on an Account
Hi,
Need to write a trigger such that if a Contact already exists ( based on email, phone or name) on the Parent Account or any of the Parents' Child accounts, then if a user tries to Create a Contact, an error should be thrown.
This trigger should be Account based and not Org.
Lets say a Contact is created on Account 'B'. Account 'A' is the parent for account B. and if i try to create same contact on Account 'C' ( another Child of Account 'A'), error should be thrown.
or also if the Contact exists on Parent and if we try to create a duplicate on any of its Child, even then an error should be thrown.
I had written a trigger but that worked only on a single account.
Thank You.
Need to write a trigger such that if a Contact already exists ( based on email, phone or name) on the Parent Account or any of the Parents' Child accounts, then if a user tries to Create a Contact, an error should be thrown.
This trigger should be Account based and not Org.
Lets say a Contact is created on Account 'B'. Account 'A' is the parent for account B. and if i try to create same contact on Account 'C' ( another Child of Account 'A'), error should be thrown.
or also if the Contact exists on Parent and if we try to create a duplicate on any of its Child, even then an error should be thrown.
I had written a trigger but that worked only on a single account.
Thank You.
I hope this helps
trigger PreventDuplicateContact on Contact (before insert,before update) {
if(trigger.isInsert || trigger.isUpdate)
{
Map<Id,List<Contact>> accMap = new Map<Id,List<Contact>>();
List<Contact> contactList;
for(Contact c: [Select Id,Email,Phone,accountId from Contact]){
if(accMap.containsKey(c.accountId)){
accMap.get(c.accountId).add(c);
}
else{
contactList = new List<Contact>();
contactList.add(c);
accMap.put(c.accountId,contactList);
}
}
for (Contact c : Trigger.new)
{
if(accMap.containsKey(c.accountId)){
for(Contact con : accMap.get(c.accountId)){
if((c.Email == con.Email || c.Phone == con.Phone) && (c.id!= con.id ) ){
// c.Email.addError('Contact with this email address already exists.Please create a Contact Role. <a href=https://cs18.salesforce.com/02Z/e?parentId='+c.accountid+'\'>Create a Contact Role</a>',false);
String baseUrl = URL.getSalesforceBaseUrl().toExternalForm()+'/02Z/e?parentId=';
string errorMsg = '<a style=\'color:1B2BE8\'href="'+baseUrl+ c.accountid +'"> Please Create a Contact Role </a>';
c.addError('This Contact already exists.'+ errorMsg,false);
}
}
}
}
}
}
what modifications can i bring intp it