You need to sign in to do that
Don't have an account?
Wik
Trigger to Prevent Duplicate Contact on Accounts
Hi ,
I have written a piece of trigger , trying to prevent duplicate contacts on an Account:
trigger PreventDuplicateContact on Contact (before insert, before update){
Set<id> accid = new Set<id>();
for (Contact c : Trigger.new)
accid.add(c.Accountid);
Map<Id,list<contact>> AccEmails = new Map<Id,list<contact>>();
Map<Id, Contact> cont = new Map<Id, Contact> ([Select Email from Contact where Id in:accid]);
for (Contact c : Trigger.new){
if ((contact.Email !=null) && (System.Trigger.isInsert ||(contact.Email != System.Trigger.oldMap.get(contact.Id).Email))){
if ( cont.containsKey(contact.Email)){
Contact co = cont.get(contact.Email);
if(co.accountid==contact.accountid){
contact.Email.addError('Another new contact has the '+'same email address.');
}else{
cont.put(contact.Email,c);
}}
}
}
for (Contact c : [SELECT Email FROM Contact WHERE Email IN :cont.KeySet()]){
Contact newContact = cont.get(c.Email);
newContact.Email.addError('A Contact with this email '+'address already exists.');
}
}
But this is not working as expected. Please help me get it modified
I have written a piece of trigger , trying to prevent duplicate contacts on an Account:
trigger PreventDuplicateContact on Contact (before insert, before update){
Set<id> accid = new Set<id>();
for (Contact c : Trigger.new)
accid.add(c.Accountid);
Map<Id,list<contact>> AccEmails = new Map<Id,list<contact>>();
Map<Id, Contact> cont = new Map<Id, Contact> ([Select Email from Contact where Id in:accid]);
for (Contact c : Trigger.new){
if ((contact.Email !=null) && (System.Trigger.isInsert ||(contact.Email != System.Trigger.oldMap.get(contact.Id).Email))){
if ( cont.containsKey(contact.Email)){
Contact co = cont.get(contact.Email);
if(co.accountid==contact.accountid){
contact.Email.addError('Another new contact has the '+'same email address.');
}else{
cont.put(contact.Email,c);
}}
}
}
for (Contact c : [SELECT Email FROM Contact WHERE Email IN :cont.KeySet()]){
Contact newContact = cont.get(c.Email);
newContact.Email.addError('A Contact with this email '+'address already exists.');
}
}
But this is not working as expected. Please help me get it modified
Please replace contact with c inside for each loop i.e (for (Contact c : Trigger.new){ ..... }) replace all "contact" with "c".
If this solves your problem, kindly mark it as the best answer.
Thanks,
Vatsal
with below query
Here is the updated Code:
If this solves your problem, kindly mark it as the best answer.
Thanks,
Vatsal
The code that you have provided works on an org wide basis, i am looking to restrict duplicate Contacts on Account basis.
If the Account is not same then in that case the duplicates should be allowed.
Any suggestions for this?
-
Create a "Duplicate Check" text field and make it unique case sensitive
-
Have a workflow populate the above field with either AccountId + Email or simply Email if you want org wide dupe prevention
Should take no more than 5 minutes!Here is the updated code:
If this solves your problem, kindly mark it as the best answer.
Thanks,
Vatsal
List <Account> acclist = new List<Account>([SELECT Id, Name from Account]);
List <Contact> conlist = new List<Contact>([SELECT Email,AccountId from Contact Where AccountId IN :acclist]);
for(Contact con: Trigger.new){
for(Contact c: conlist){
if(con.Email == c.Email){
con.Email.addError('Contact with this email address already exists');
}
}
}
}
1. Create field isDuplicate on Contact
2. Mark this flag as duplicate on insert/update if any other existing contact has same website.
3. Mobile cannot be duplicate for same account. Create trigger based validation.
could you please tell me the meaning of this line(line no. 10) in your code - "accMap.get(c.accountId).add(c);".
Your code is perfectly working but, i am new in development so please tell me or write this line in another way(without shortform) so that i can understand.