function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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?

Abbas RazaAbbas Raza
Hi guys try this to prevent duplicates record on the basis of email.

list<Lead> LE=[Select Email from Lead];
        map<String,Lead> mapLead=new map<String,Lead>();
        
        //Lead    
        for(Lead l:LE)
        {
            mapLead.put(l.Email, l);
        }
        for(Lead l1:leads)
        {
            if(l1.email !=null && String.isNotBlank(l1.Email))
            {
                if(mapLead.containsKey(l1.Email))
                {
                    l1.addError('same Email Exists Please try different one ');
                }
                
            }
        }