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
Vidya H 4Vidya H 4 

Need to Get the count of Contacts on Account level using Trigger. And trigger should work in all contexts.

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vidya,

Can you try the trigger logic on contact object.
 
trigger countContacts on Contact (after insert, after update, after delete) 
{
    list<contact> conList = new list<contact>();
    list<account> accList = new list<account>();
    set<ID> accIDs = new set<ID>();
    if(trigger.isInsert || trigger.isUpdate)
    {
        for(contact con : trigger.new)
        {
            if(String.isNotBlank(con.accountId))
            {
                accIds.add(con.accountId);
            }
        }
    }
    if(trigger.isDelete)
    {
        for(contact con : trigger.old)
        {
            accIDs.add(con.AccountId);
        }
    }
    if(accIDs.size()>0)
    {
        conList = [select name, id, accountId from contact where accountId IN:accIDs];
        accList = [select name, id , number_of_contacts__c from account where ID IN:accIDs];
    }
    for(account acc : accList)
    {
       acc.Number_of_contacts__c = conList.size();
    }
    update accList;

}

If this solution helps, Please mark it as best answer.

Thanks,
 
Vidya H 4Vidya H 4

@Sai Praveen  need to write trigger using handler class
 
CharuDuttCharuDutt
Hii Vidya
Try Below Class
trigger NumberOfChild on Contact (After Insert,After Update,After Delete) {
    If(trigger.isAfter &&Trigger.isInsert){
        NumberOfChildTriggerHandler.insertMethod(trigger.new);
    }
    If(trigger.isAfter &&Trigger.isUpdate){
        NumberOfChildTriggerHandler.updateMethod(trigger.new,trigger.oldMap);
    }
     If(trigger.isAfter &&Trigger.isDelete){
        NumberOfChildTriggerHandler.deleteMethod(trigger.old);
    }
}

#############################################################################################################

Handler Class
public class NumberOfChildTriggerHandler {
 
    public static Void InsertMethod(list<Contact> lstCon ){
        List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
         for(Contact con : lstCon){ 
            if(con.AccountId != null){
                   setAccIds.add(con.AccountId);      
            }   
        }
        for(Account acc :[Select id,Total_Contacts__c ,(Select id,name from contacts) from Account where Id in : setAccIds]){
     
        acc.Total_Contacts__c = acc.contacts.size();
       
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList; 
    }
    }
    public static Void UpdateMethod(list<Contact> lstCon,map<Id,Contact>oldmap ){
        List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
         for(Contact con : lstCon){ 
            if(con.AccountId != null && con.AccountId != oldMap.get(con.Id).AccountId){
                   setAccIds.add(con.AccountId); 
                setAccIds.add(oldMap.get(con.Id).AccountId);
            }   
        }
        for(Account acc :[Select id,Total_Contacts__c ,(Select id,name from contacts) from Account where Id in : setAccIds]){
     
        acc.Total_Contacts__c = acc.contacts.size();
       
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList; 
    }
    }
    public static Void deleteMethod(list<Contact> lstCon){
        List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
         for(Contact con : lstCon){ 
            if(con.AccountId != null){
                   setAccIds.add(con.AccountId);      
            }   
        }
        for(Account acc :[Select id,Total_Contacts__c ,(Select id,name from contacts) from Account where Id in : setAccIds]){
     
        acc.Total_Contacts__c = acc.contacts.size();
       
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList; 
    }
    }
}
Please Mark It As Best Asnwer If It Helps
Thank You!