Posts

Showing posts from July, 2018

Restricting the user to create duplicate email(Contact) for a particular account

Apex class -------------- public class DuplicationOnContact {     public static void doDuplicateCheck(List<Contact> conlist){          Map<String,String> mapcon=new Map<String,String>();         set<String> setacc=new set<String>();         for(Contact con:conlist){             if(con.AccountId!=null)                 setacc.add(con.AccountId);                 }       for(Contact con:[select Id,Name,Email,AccountId from Contact where AccountId=:setacc]){          mapcon.put(con.Email,con.AccountId);     }     for(Contact con:conlist){         if(!mapcon.isEmpty() &&  mapcon.containskey(con.Email)){             con.addError('Please enter unique Email');         }     }     } } Trigger ---------- Trigger ContactTrigger on Contact(before insert,before update){        DuplicationOnContact.doDuplicateCheck(Trigger.new);    } //It will be fired when the user creates a contact with email(which already present in the d