+ Start a Discussion
WikWik 

Prevent Duplicate Contact on Accounts

Hi,

Below is a piece of trigger to prevent the creation of Duplicate on whole org basis,

trigger ContactDuplicatePreventer on Contact(before insert, before update){
   Map<String, Contact> contactMap =new Map<String, Contact>();
    for (Contact contact : System.Trigger.new){
if ((contact.Email !=null) && (System.Trigger.isInsert ||(contact.Email != System.Trigger.oldMap.get(contact.Id).Email))){
  if (contactMap.containsKey(contact.Email)){
contact.Email.addError('Another new contact has the '+'same email address.');
            }else{
                contactMap.put(contact.Email, contact);
     }
       }
    }
    for (Contact contact : [SELECT Email FROM Contact WHERE Email IN :contactMap.KeySet()]){
        Contact newContact = contactMap.get(contact.Email);
        newContact.Email.addError('A Contact with this email '+'address already exists.');
   }
}

But i am looking to restrict duplicates only on Account records i.e. it should prevent duplicate contacts in case the the duplicate is on the same Account, if Account is not the same then it should allow duplicates.

Thank You

Best Answer chosen by Wik
David "w00t!" LiuDavid "w00t!" Liu
You can actually do this without code in about 5 minutes:

1. Create a "Duplicate Key" text field and mark the field as unique case sensitive
2. Have a workflow populate that field with Email OR AccountId + Email

Since existing records won't have the Duplicate Key field populated, make sure to trigger the workflow on them to get things going.

All Answers

ShashForceShashForce
Hi,

To do that, you should create a map of accountId and its list of contacts, something like

Map<Id,list<contact>> AccEmails = new Map<Id,list<contact>();
Ramu_SFDCRamu_SFDC
Add one more if condition after 'if (contactMap.containsKey(contact.Email)){' and check if the account id's are same for the newly inserted contact and the existing contact record.

Contact c=contactmap.get(contact.Email);
if(c.accountid==contact.accountid){

contact.Email.addError('Another new contact has the '+'same email address.');
            }else{
                contactMap.put(contact.Email, contact);
              }

see if this helps !!
WikWik

Do You mean something like this

 

trigger ContactDuplicatePreventers on Contact(before insert, before update){
   Map<String, Contact> contactMap =new Map<String, Contact>();
    for (Contact contact : System.Trigger.new){
if ((contact.Email !=null) && (System.Trigger.isInsert ||(contact.Email != System.Trigger.oldMap.get(contact.Id).Email))){
  if (contactMap.containsKey(contact.Email)){
  Contact c=contactmap.get(contact.Email);
if(c.accountid==contact.accountid)
contact.Email.addError('Another new contact has the '+'same email address.');
            }else{
                contactMap.put(contact.Email, contact);
     }
       }
    }
    for (Contact contact : [SELECT Email FROM Contact WHERE Email IN :contactMap.KeySet()]){
        Contact newContact = contactMap.get(contact.Email);
        newContact.Email.addError('A Contact with this email '+'address already exists.');
   }
}

But it's still working on whole org basis not just the account

David "w00t!" LiuDavid "w00t!" Liu
You can actually do this without code in about 5 minutes:

1. Create a "Duplicate Key" text field and mark the field as unique case sensitive
2. Have a workflow populate that field with Email OR AccountId + Email

Since existing records won't have the Duplicate Key field populated, make sure to trigger the workflow on them to get things going.

This was selected as the best answer
KeerthigeeKeerthigee
Hi,

Try this code once

Duplicate2 onContact (beforeinsert, beforeupdate) {
{
set
<string> setFirstNames=newset<string>();
set
<string> setLastNames=newset<string>();
list
<contact> listnames=newlist<contact>();
for
(contact acc:trigger.new)
{
setFirstNames.add(acc.FirstName);
setLastNames.add(acc.LastName);
}
listnames=[
select FirstName,LastName,id from contact where FirstName in:setFirstNames and LastName in:setLastNames];
if
(trigger.isinsert)
{
for
(contact acc:trigger.new)
{
for
(contact dup:listnames)
{
if
(acc.FirstName==dup.FirstName && acc.LastName==dup.LastName )
acc.adderror(
'le contact existe déja(already exists');
}
}
}
if
(trigger.isupdate)
{
for
(contact acc:trigger.new)
{
for
(contact dup:listnames)
{
if
(acc.FirstName==dup.FirstName && acc.FirstName !=trigger.oldmap.get(acc.id).FirstName && acc.LastName==dup.LastName && acc.LastName !=trigger.oldmap.get(acc.id).LastName)
acc.adderror('le contact existe déja(already exists)');
}
}
}
}
}

If the above code resolves the problem,please mark it as a best answer.
WikWik

Hey David

Thanx a ton

Your solution works perfect without writting a trigger.

Is it possible to modify the Error message that pops up?