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
Sriram Varma 1Sriram Varma 1 

triggers salesforce

Create field called "Count of Contacts" on Account Object. 
When we add the Contacts for that Account then count will populate in the field on Account details page. When we delete the Contacts for that Account, then Count will update automatically.
Malika Pathak 9Malika Pathak 9

Hi Sriram,

Find the below Solution

TRIGGER

trigger NumberOfContactAssociated on Contact (After insert,After Update,After Delete) 
{
    ContactAssociated_Handler.CountUpdated(Trigger.new);
    System.debug('Trigger is running');
}


Apex class​​​​​​​

 

public class ContactAssociated_Handler
{
    public static void CountUpdated(List<Contact> conList)
    {
        System.debug('conList>>>>>>>>'+conList);
        Set<id> AccIdSet = new Set<id>();
        for(Contact con:conList)
        {
            AccIdSet.add(con.AccountId);
        }       
        List<Account> accList = [select Id,Name,Count_Of_Contacts__c,Rollup_Amount_X__c from Account where Id  in:AccIdSet];
        
        System.debug('accList>>>>>>>>'+accList);
        Map<id,List<COntact>> AccToConMap = new Map<id,List<COntact>>();
        for(Contact con:[select id ,AccountId,Amount_X__c from Contact Where AccountId IN :AccIdSet])
        {
            if(!AccToConMap.containskey(con.AccountId))
            {
                AccToConMap.put(con.AccountId,new List<Contact>());
            }
            AccToConMap.get(con.AccountId).add(con);
        }
        for(Account acc:accList)
        {
            if(AccToConMap.containskey(acc.id))
            {
                acc.Count_Of_Contacts__c = AccToConMap.get(acc.Id).size();                            
            }
            
        }
        update accList;
        System.debug('Updated conLiss>>>>'+accList);
                
    }
}

if you find this helpful mark it as the best answer.
Ajay Patel 54Ajay Patel 54

Hope these will help you in all the condition

//Apex Class triggerhNDLER

public class NoOfContactEventHandlerClass {

    public static void counttrigger(contact [] cts){
        set<id> accidlist = new set<id>();
        for(contact con:cts){
            accidlist.add(con.Accountid);
        }
        list<account> acc = new list<account>();
        for(Account a:[select id,Count_No_of_Contact__c,(select id from contacts)from Account where id IN:accidlist]){
            a.Count_No_of_Contact__c = a.contacts.size();
            acc.add(a);
        }
        update acc;
    }
}




///Trigger on contact

trigger ContactCountOnAccount on Contact (after insert,after update,after delete,after undelete) {
        if(trigger.isafter){
            if(trigger.isinsert){
                NoOfContactEventHandlerClass .counttrigger(trigger.new);
            }
        }
        if(trigger.isafter){
            if(trigger.isundelete){
                NoOfContactEventHandlerClass .counttrigger(trigger.new);
            }
            
        }
        if(trigger.isafter){
            if(trigger.isupdate){
                NoOfContactEventHandlerClass .counttrigger(trigger.new);
            }
        }
        if(trigger.isafter){
            if(trigger.isupdate){
                NoOfContactEventHandlerClass .counttrigger(trigger.old);
            }
        }
        if(trigger.isafter){
            if(trigger.isdelete){
                NoOfContactEventHandlerClass .counttrigger(trigger.old);
            }
        }       
}


Thanks 
Ajay Patel