You need to sign in to do that
Don't have an account?
Issue with the Trigger
Hi,
the following trigger was designed to prevent duplicate contacts on an account. But even when i edit the same contact and click save, the trigger acts and does not let me save the contact.
please help me figure out whats wrong with the trigger
trigger PreventDuplicateContact on Contact (after insert,after update) {
if(trigger.isInsert || trigger.isUpdate)
{
Map<Id,List<Contact>> accMap = new Map<Id,List<Contact>>();
List<Contact> contactList;
for(Contact c: [Select Id,Email,accountId from Contact]){
if(accMap.containsKey(c.accountId)){
accMap.get(c.accountId).add(c);
}
else{
contactList = new List<Contact>();
contactList.add(c);
accMap.put(c.accountId,contactList);
}
}
for (Contact c : Trigger.new)
{
if(accMap.containsKey(c.accountId)){
for(Contact con : accMap.get(c.accountId)){
if(c.Email == con.Email){
// c.Email.addError('Contact with this email address already exists.Please create a Contact Role. <a href=https://cs18.salesforce.com/02Z/e?parentId='+c.accountid+'\'>Create a Contact Role</a>',false);
String baseUrl = URL.getSalesforceBaseUrl().toExternalForm()+'/02Z/e?parentId=';
string errorMsg = '<a style=\'color:1B2BE8\'href="'+baseUrl+ c.accountid +'"> Create a Contact Role </a>';
c.Email.addError('Contact with this email address already exists.Please create a Contact Role'+ errorMsg,false);
}
}
}
}
}
}
So you should check that the 2 contacts with the same email don't have the same Id, to be sure it's different records.
Two other things :
-
You should filter your account list to only query the ones related to the contacts you are saving, otherwise you will query all the Contact table for nothing. This may probably hit a governor limit in the future
-
Instead of creating a map with a for loop, you can query the Account table and get the related Contact list like this : Select Id, (Select Id, Email From Contacts) From Account. Then you can loop through your accounts, and inside loop again on your related Contacts :
for (Account acc : [Select Id, (Select Id, Email From Contacts) From Account]){
for (Contact c : acc.Contacts)
{
// Do something with c.Id and c.email
}
}
All Answers
what is happening here is let's say i created a Contact. Now when i go and try and edit this contact to update some information, even then the trigger is acting and this does not let me save the update to contact.
this should not have happened actually.
So you should check that the 2 contacts with the same email don't have the same Id, to be sure it's different records.
Two other things :
-
You should filter your account list to only query the ones related to the contacts you are saving, otherwise you will query all the Contact table for nothing. This may probably hit a governor limit in the future
-
Instead of creating a map with a for loop, you can query the Account table and get the related Contact list like this : Select Id, (Select Id, Email From Contacts) From Account. Then you can loop through your accounts, and inside loop again on your related Contacts :
for (Account acc : [Select Id, (Select Id, Email From Contacts) From Account]){
for (Contact c : acc.Contacts)
{
// Do something with c.Id and c.email
}
}
Perhaps you should code an after Insert trigger to test new records only.