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

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
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
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>();
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 !!
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
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.
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.
Hey David
Thanx a ton
Your solution works perfect without writting a trigger.
Is it possible to modify the Error message that pops up?
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 ');
}
}
}