Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
Hi ,
Please refer below link which has code and explanation for the same thing.
http://www.dhruvsoft.com/blog/displaying-total-number-of-contacts-for-each-account-in-salesforce/
Hope this helps you!
Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
Thanks and Regards
Sandhya
All Answers
Use below trigger.
Thanks,
Swaraj
Hi ,
Please refer below link which has code and explanation for the same thing.
http://www.dhruvsoft.com/blog/displaying-total-number-of-contacts-for-each-account-in-salesforce/
Hope this helps you!
Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
Thanks and Regards
Sandhya
Use this code it works on update you can also change Trigger Event :
trigger CountContact on Contact (after update) {
set<id>SetofAccountID = new Set <id>();
Map<id,List<Contact>> MapofAccountWithContact = new map <id,List<Contact>>();
List<Account> ListofAccount = new List<Account>();
if(Trigger.isafter && trigger.isupdate){
for(Contact Con :trigger.new){
if(Con.Accountid != null ){
SetofAccountID.add(Con.Accountid);
}
}
if(SetofAccountID != null && SetofAccountID.size()>0){
for(Contact con :[select id , accountid from Contact where AccountId IN: SetofAccountID ]){
if(MapofAccountWithContact.get(con.AccountID) == null){
MapofAccountWithContact.put(con.AccountID, new List<contact>()) ;
}
MapofAccountWithContact.get(con.accountid).add(con);
}
}
if(MapofAccountWithContact.size()>0){
for(Account acc : [select id ,No_of_Contact__c from Account where id IN: MapofAccountWithContact.keyset()]){
for(Contact Con :trigger.new){
if(Con.Accountid != null && MapofAccountWithContact.containsKey(Con.AccountID)){
acc.No_of_Contact__c =MapofAccountWithContact.get(Con.Accountid).size();
ListofAccount.add(acc);
}
}
}
}
if(ListofAccount.size()>0){
update ListofAccount;
}
}
}