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

Restrict creation of duplicate contacts for an Account
I have a requirement that for an account there should be unique email id for every related contact. But in my code it is not working as per the requirement, it is checking all the contacts irrespective of Account.
But I need to check only Account related contacts email id's.Here is my code please help me where I am doing wrong.
trigger ContactTrigger on Contact (before insert,before update)
{
set<ID> accId = new set<ID>();
list<string> ContactEmails=new list<string>();
for(Contact conVar:trigger.new)
{
ContactEmails.add(conVar.email);
accId.add(conVar.AccountId);
}
system.debug('ContactEmails ************' +ContactEmails);
list<Contact> listOfDuplicateContacts=[select id,email,Account.ID from Contact where email in :ContactEmails AND AccountID IN : accId];
system.debug('listOfDuplicateContacts ************' +listOfDuplicateContacts);
for(Contact con:trigger.new)
{
if(trigger.isInsert){
if(listOfDuplicateContacts.size()!=0)
{
con.addError('Contact email already exists with this name');
}
}
if(trigger.isUpdate)
{
for(Contact oldContact :trigger.old)
{
if(con.Email!=oldContact.Email && listOfDuplicateContacts.size()!=0)
{
con.addError('Contact email already exists with this name');
}
}
}
}
}
But I need to check only Account related contacts email id's.Here is my code please help me where I am doing wrong.
trigger ContactTrigger on Contact (before insert,before update)
{
set<ID> accId = new set<ID>();
list<string> ContactEmails=new list<string>();
for(Contact conVar:trigger.new)
{
ContactEmails.add(conVar.email);
accId.add(conVar.AccountId);
}
system.debug('ContactEmails ************' +ContactEmails);
list<Contact> listOfDuplicateContacts=[select id,email,Account.ID from Contact where email in :ContactEmails AND AccountID IN : accId];
system.debug('listOfDuplicateContacts ************' +listOfDuplicateContacts);
for(Contact con:trigger.new)
{
if(trigger.isInsert){
if(listOfDuplicateContacts.size()!=0)
{
con.addError('Contact email already exists with this name');
}
}
if(trigger.isUpdate)
{
for(Contact oldContact :trigger.old)
{
if(con.Email!=oldContact.Email && listOfDuplicateContacts.size()!=0)
{
con.addError('Contact email already exists with this name');
}
}
}
}
}
I did some refactoration of your code . Please try this , it should work fine
All Answers
You should check the relationships between account and email. Currently you are just querying all matching email addresses from the accounts and not checking to which account does the email belong to. Try this:
Regards,
MKR
I did some refactoration of your code . Please try this , it should work fine
Thank you very much. Could you please elaborate me the logic in this trigger in detail,as I am new to apex coding.
In Update case , we are only runnig the logic if contact and email is changed.
Also checking the setCheckDuplicateinSameList same list , if some one tries to insert csv with duplicate email .