Hello Srikanth,1.Create a field called 'No of contacts' on account. 2.Use this trigger.
If it helps make it as best answer.Thanks,Dilip.trigger CountContactsnew on Contact (after insert, after delete, after undelete) {
List<id> accIdList = new List<id>();
if(Trigger.isInsert || Trigger.isUndelete){
For(Contact con1 : Trigger.new){
accIdList.add(con1.accountid);
}
}
if(Trigger.isDelete){
For(Contact con1 : Trigger.old){
accIdList.add(con1.accountid);
}
}
List<Account> accUpdateList = new List<Account>();
For(Account acc : [SELECT No_Of_Contacts__C,(SELECT id FROM Contacts) FROM Account WHERE id =: accIdList]){
acc.No_Of_Contacts__C = acc.Contacts.size();
accUpdateList.add(acc);
}
try{
update accUpdateList;
}Catch(Exception e){
System.debug('Exception :'+e.getMessage());
}
}
3 answers