You need to sign in to do that
Don't have an account?
@ M Coder
Duplication trigger on contact at account level - please modify my code
can anyone please modify my code .
Under an account , there should be only one contact with name phone . if i create one more contact with same name then the error shoulp appear . my code :
trigger PreventDuplicateContactMap on Contact (before insert,before update) {
if(trigger.isInsert || trigger.isUpdate)
{
Map<Id,List<Contact>> accMap = new Map<Id,List<Contact>>();
List<Contact> contactList;
for(Contact c: [Select Id,LastName,phone,Email,accountId from Contact where AccountId !=Null ]){
if(accMap.containsKey(c.accountId)){
accMap.get(c.accountId).add(c);
system.debug('If loop ===');
}
else{
system.debug('else loop ===');
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.Phone== con.Phone) || ( c.LastName == con.LastName ) )
{
c.addError('Duplicate Contact.......... ');
}
}
}
}
}
}
Under an account , there should be only one contact with name phone . if i create one more contact with same name then the error shoulp appear . my code :
trigger PreventDuplicateContactMap on Contact (before insert,before update) {
if(trigger.isInsert || trigger.isUpdate)
{
Map<Id,List<Contact>> accMap = new Map<Id,List<Contact>>();
List<Contact> contactList;
for(Contact c: [Select Id,LastName,phone,Email,accountId from Contact where AccountId !=Null ]){
if(accMap.containsKey(c.accountId)){
accMap.get(c.accountId).add(c);
system.debug('If loop ===');
}
else{
system.debug('else loop ===');
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.Phone== con.Phone) || ( c.LastName == con.LastName ) )
{
c.addError('Duplicate Contact.......... ');
}
}
}
}
}
}
I updated your code. Below should work. It will prevent two contacts for a single Account having same last name or same Phone number.
Please let me know for any issues or questions. Thanks.
All Answers
Below is the sample code for duplicate emails, you can change accordingly.
Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
Best Regards
Sandhya
I updated your code. Below should work. It will prevent two contacts for a single Account having same last name or same Phone number.
Please let me know for any issues or questions. Thanks.