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

Trigger - Help
Hi,
Need to write a trigger such that if a Contact already exists ( based on email, phone or name) on the Parent Account or any of the Parents' Child accounts, then if a user tries to Create a Contact, an error should be thrown.
This trigger should be Account based and not Org.
Thank You.
i give you a sample code for check duplicate value in trigger .....its useful for you if you find any issue then let me know .....
trigger StagesDuplicateM1 on Stages__c (before insert, before update) {
Map<String, Stages__c> StagesMap = new Map<String, Stages__c>();
for (Stages__c Stages : System.Trigger.new)
{
if ((Stages.M1__c != null) &&
(System.Trigger.isInsert ||(Stages.M1__c !=System.Trigger.oldMap.get(Stages.Id).M1__c))) {
StagesMap.put(Stages.M1__c, Stages);
}
if ((Stages.M2__c != null) &&
(System.Trigger.isInsert ||(Stages.M2__c !=System.Trigger.oldMap.get(Stages.Id).M2__c))) {
StagesMap.put(Stages.M2__c, Stages);
}
if ((Stages.M3__c != null) &&
(System.Trigger.isInsert ||(Stages.M3__c !=System.Trigger.oldMap.get(Stages.Id).M3__c))) {
StagesMap.put(Stages.M3__c, Stages);
}
}
for (Stages__c Stages : [SELECT M1__c FROM Stages__c WHERE M1__c IN :StagesMap.KeySet()])
{
Stages__c newStages = StagesMap.get(Stages.M1__c);
newStages.M1__c.addError(Stages.M1__c + ' is ssaved here');
}
for (Stages__c Stages : [SELECT M2__c FROM Stages__c WHERE M2__c IN :StagesMap.KeySet()])
{
Stages__c newStages = StagesMap.get(Stages.M2__c);
newStages.M2__c.addError(Stages.M2__c + ' is saved here');
}
for (Stages__c Stages : [SELECT M3__c FROM Stages__c WHERE M3__c IN :StagesMap.KeySet()])
{
Stages__c newStages = StagesMap.get(Stages.M3__c);
newStages.M3__c.addError(Stages.M3__c + ' is saved here ');
}
}
trigger PreventDuplicateContact 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,Email,Phone,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.Phone == con.Phone) && (c.id!= con.id ) ){
// 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 +'"> Please Create a Contact Role </a>';
c.addError('This Contact already exists.'+ errorMsg,false);
}
}
}
}
}
}
You can refer below code: Above code will check Account based duplicate contact based on Email.
If this solves your problem, kindly mark it as the best answer.
Thanks,
Vatsal
"Need to write a trigger such that if a Contact already exists ( based on email, phone or name) on the Parent Account or any of the Parents' Child accounts, then if a user tries to Create a Contact, an error should be thrown.
This trigger should be Account based and not Org. "
If you try to create duplicate contact on different account than it will not give you any error.
Lets say a Contact is created on Account 'B'. Account 'A' is the parent for account B. and if i try to create same contact on Account 'C' ( another Child of Account 'A'), error should be thrown.