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
Bhuvanakruthi MudholeBhuvanakruthi Mudhole 

)Create a field 'Total Contact' on Account and populate it with the total number of contact associated with that account.

Akshay_DhimanAkshay_Dhiman
 Hi Bhuvanakruthi,
 Try this code to fetch a number of contacts in Account 
 
trigger CountOfContact on Contact (after insert) {
    //NumberofLocations__c
    Set<Id> aId = new Set<Id>();
    List<Account> accToUpdate= new List<Account>();
    map<id,List<Contact>> conMap = new map<id,List<Contact>>();
    for(Contact c :trigger.New){
        if(c.accountId!=null){
            if(conMap.get(c.AccountId)==null){
                conMap.put(c.AccountId,new List<Contact>());
            }
            conMap.get(c.AccountId).add(c);
            aId.add(c.AccountId);
        }
    }
    if(aId.size() > 0){
        List<Account> accList=[select name,Total_Contact__c from Account where id in:aId];
        for(Account a : accList){
            
            a.Total_Contact__c= conMap.get(a.Id).size();
            accToUpdate.add(a);
        }
        update accToUpdate;
    }


 If found this helpful mark it as best so that it helps others in solving the same.

 Thanks 
 Akshay
Ajay K DubediAjay K Dubedi
Hi Bhuvanakruthi,

Please try the below code. Hope it helps you.

It will work for all the operations delete, insert, update.

you need to replace this field with "NumberContacts__c" your custom field "Total_Contact__c"
 
trigger updateContactCountOnAccount on Contact (after insert, after update, after delete, after undelete) {

Set<Id> AccountIds = new Set<Id>();

if(!Trigger.isDelete){
    for (Contact ct : Trigger.new) {        

        if(Trigger.isInsert && ct.AccountId != null){
            AccountIds.add(ct.AccountId);
        }
 //For Contact Update scenarios, Contact.AccountId value should be checked for null in new and old record.
        if(Trigger.isUpdate){
            if(ct.AccountId==null && Trigger.oldMap.get(ct.Id).AccountId != null){
                AccountIds.add(Trigger.oldMap.get(ct.Id).AccountId);
            }
            if(ct.AccountId!=null && Trigger.oldMap.get(ct.Id).AccountId != null && ct.AccountId != Trigger.oldMap.get(ct.Id).AccountId){
                AccountIds.add(ct.AccountId);
                AccountIds.add(Trigger.oldMap.get(ct.Id).AccountId);
            }
            if(ct.AccountId!=null && Trigger.oldMap.get(ct.Id).AccountId == null){
                AccountIds.add(ct.AccountId);
            }
        }

        if(Trigger.isUndelete && ct.AccountId != null){
            AccountIds.add(ct.AccountId);
        }
    }
}else{
    for (Contact ct : Trigger.old){
        if(Trigger.isDelete && ct.AccountId != null){
            AccountIds.add(ct.AccountId);
        }
    }   
}

List<Account> AcctToUpdate = new List<Account>();
//AggregateResult to get count of Contact for each Account
for (AggregateResult ar: [Select Count(Id) ContactCount, AccountId from Contact where AccountId IN: AccountIds GROUP BY AccountId]){
    Account tmp = new Account(Id=(Id)ar.get('AccountId'), NumberContacts__c=(Decimal)ar.get('ContactCount'));
    AcctToUpdate.add(tmp);
}
if(AcctToUpdate.size()>0) update AcctToUpdate;
 }
 
Please select as best answer if it helps you.

Thank You,
Ajay Dubedi