function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
THUNDER CLOUDTHUNDER CLOUD 

How to display number of contacts in Account page ?

How to display number of contact records associated with account in account page ?
Best Answer chosen by THUNDER CLOUD
SandhyaSandhya (Salesforce Developers) 

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

Swaraj Behera 7Swaraj Behera 7
Hi,
Use below trigger.
 
trigger noofcontacts on Contact (after insert,after delete) {


set<id>accid=new set<id>();

list<contact>contactlist=new list<contact>();
list<contact>listcon=new list<contact>();
list<account>acclist=new list<account>();
list<account>listacc=new list<account>();
map<id,integer>mapCount=new map<id,integer>();


if(trigger.isinsert)                              //insert   con
{
for(contact con:trigger.new)
{
accid.add(con.accountid);
}
}

if (trigger.isdelete)                          //delete  con
{
for(contact con:trigger.old)
{
accid.add(con.accountid);
}
}

acclist=[SELECT id,name FROM account WHERE id in:accid];
contactlist=[SELECT id,name,accountid FROM contact WHERE accountid in:accid];

for(account acc:acclist){
listcon.clear();
for(contact c:contactlist){
if(c.accountid==acc.id){
listcon.add(c);

mapCount.put(c.accountid,listcon.size());
}
}
}

if(acclist.size()>0){

for(Account a:acclist)
{
if(mapCount.get(a.id)==null)
a.No_of_Contact__c =0;

else

a.No_of_Contact__c =mapCount.get(a.id);
listacc.add(a);
}
}


if(listacc.size()>0)

update listacc;
}

Thanks,
Swaraj
SandhyaSandhya (Salesforce Developers) 

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



 
This was selected as the best answer
Prabhat gangwarPrabhat gangwar

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;
        }
    }
}