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
Admin 123Admin 123 

To display sum of all contact for its respective Account in its Parent Account.

Account and Contact Object have Lookup relationship.

The Requirement is:

ParentAccount: ABC has
Account1: Contact1, Contact2 (count of contact in account will display 2 in Account custom field)
ParentAcc: ABC has
Account2: Contact1, Contact2, Contact3(count of contact in account will display 3 in Account custom field)

But now In Parent Account(in its custom field): ABC Count Should Display as 5.

How can this be achieved through Apex code?
Raj VakatiRaj Vakati
Hi , 
You need to write a trigger on contact on After insert and after update and before delete events 

Thanks ,
Raj


 
Admin 123Admin 123
Are you sure i will need to write on Contact, Coz I want the sum of all Accounts(having total contact) in Parent Account? 
HARSHIL U PARIKHHARSHIL U PARIKH
This are the steps you can follow,

1) First make a field on Account Named Total_Contacts__c
2) Write / Copy Paste a following trigger on Contact object.
Trigger Code:
 
Trigger ContactRecordCount on Contact(After Insert, After Update, After Delete, After UnDelete)
{

    List<ID> AccountIDs = New List<ID>();
    List<Id> paretAccountIds = New List<Id>();
 
    IF(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete)
    {
        For(Contact con: Trigger.New){
            AccountIDs.add(con.AccountID);
        }
    }
    IF(Trigger.IsDelete)
    {
        For(Contact con: Trigger.Old){
            AccountIDs.add(con.AccountID);
        }
    }
    
    List<Account> AccountListToUpdate         = New List<Account>();
    List<Account> ParentAccountListToUpdate   = New List<Account>();
    Integer I = 0;
    Integer Y = 0;
    For(Account act : [Select ID, Total_Contacts__c, ParentId, 
                                (Select ID FROM Contacts) 
                                        FROM Account WHERE ID = :AccountIDs])
    {
        act.Total_Contacts__c = act.Contacts.size();
        AccountListToUpdate.add(act);
        I = act.Contacts.size();
        
        If(act.ParentId != null)
        {
            paretAccountIds.add(act.ParentId);
        } 
    }
    System.debug('The size of paretAccountIds Is: ' + paretAccountIds.size());
    
    For(Account ParentAccount : [Select Id, Name, Total_Contacts__c, 
                                            (Select Id FROM Contacts)
                                                        FROM Account WHERE Id =:paretAccountIds])
    {        
        I = ParentAccount.Contacts.size();
        List<Contact> allAssociatedConsForChildAccounts = [Select Id FROM Contact WHERE Account.ParentId =:paretAccountIds];
        Y = allAssociatedConsForChildAccounts.size();
        ParentAccount.Total_Contacts__c = I + Y;
        ParentAccountListToUpdate.add(ParentAccount);
    }
   
   
    try
    {
        If(!AccountListToUpdate.IsEmpty())
        {
            Update AccountListToUpdate;
        }
        
        If(!ParentAccountListToUpdate.IsEmpty())
        {
            update ParentAccountListToUpdate;
        }     
    }
    
    Catch(Exception e)
    {
        System.Debug('Exception Thrown: ' + e.getMessage());
    }
}
Hopr this helps!
 
HARSHIL U PARIKHHARSHIL U PARIKH
Actually Use this instead:
 
Trigger ContactRecordCount on Contact(After Insert, After Update, After Delete, After UnDelete)
{

    List<ID> AccountIDs = New List<ID>();
    List<Id> paretAccountIds = New List<Id>();
 
    IF(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete)
    {
        For(Contact con: Trigger.New){
            AccountIDs.add(con.AccountID);
        }
    }
    IF(Trigger.IsDelete)
    {
        For(Contact con: Trigger.Old){
            AccountIDs.add(con.AccountID);
        }
    }
    
    List<Account> AccountListToUpdate         = New List<Account>();
    List<Account> ParentAccountListToUpdate   = New List<Account>();
    Integer I = 0;
    Integer Y = 0;
    For(Account act : [Select ID, Total_Contacts__c, ParentId, 
                                (Select ID FROM Contacts) 
                                        FROM Account WHERE ID = :AccountIDs])
    {
        //act.Total_Contacts__c = act.Contacts.size();
        //AccountListToUpdate.add(act);
        //I = act.Contacts.size();
        
        If(act.ParentId != null)
        {
            act.Total_Contacts__c = act.Contacts.size();
            AccountListToUpdate.add(act);
            I = act.Contacts.size();
            paretAccountIds.add(act.ParentId);
        }
        else
        {
               // Do nothing.         
        }
    }
    
    For(Account ParentAccount : [Select Id, Name, Total_Contacts__c, 
                                            (Select Id FROM Contacts)
                                                        FROM Account WHERE Id =:paretAccountIds])
    {        
        I = ParentAccount.Contacts.size();
        List<Contact> allAssociatedConsForChildAccounts = [Select Id FROM Contact WHERE Account.ParentId =:paretAccountIds];
        Y = allAssociatedConsForChildAccounts.size();
        ParentAccount.Total_Contacts__c = I + Y;
        ParentAccountListToUpdate.add(ParentAccount);
    }
   
   
    try
    {
        If(!AccountListToUpdate.IsEmpty())
        {
            Update AccountListToUpdate;
        }
        
        If(!ParentAccountListToUpdate.IsEmpty())
        {
            update ParentAccountListToUpdate;
        }     
    }
    
    Catch(Exception e)
    {
        System.Debug('Exception Thrown: ' + e.getMessage());
    }
}

 
HARSHIL U PARIKHHARSHIL U PARIKH
I am sorry I am just trying to make this best possiable for you here and this is the final version I have came up with which is working for all conditions that I have tested for.
 
Trigger ContactRecordCount on Contact(After Insert, After Update, After Delete, After UnDelete)
{

    List<ID> AccountIDs = New List<ID>();
    List<Id> paretAccountIds = New List<Id>();
 
    IF(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete)
    {
        For(Contact con: Trigger.New){
            AccountIDs.add(con.AccountID);
        }
    }
    IF(Trigger.IsDelete)
    {
        For(Contact con: Trigger.Old){
            AccountIDs.add(con.AccountID);
        }
    }
    
    List<Account> AccountListToUpdate         = New List<Account>();
    List<Account> ParentAccountListToUpdate   = New List<Account>();
    Integer I = 0;
    Integer Y = 0;

    For(Account act : [Select ID, Total_Contacts__c, ParentId, 
                                (Select ID FROM Contacts) 
                                        FROM Account WHERE ID = :AccountIDs])
    {
        If(act.ParentId  ==  null)
        {
            List<Contact> fetchingallChildActsCons = [Select Id FROM Contact WHERE Account.ParentId =:act.Id];
            act.Total_Contacts__c = act.Contacts.size() + fetchingallChildActsCons.size();
            AccountListToUpdate.add(act);
        }
        
        I = act.Contacts.size();
        
        If (act.ParentId != null)
        {
            paretAccountIds.add(act.ParentId);
            act.Total_Contacts__c = act.Contacts.size();
            AccountListToUpdate.add(act);
        } 
    }
    
    For(Account ParentAccount : [Select Id, Name, Total_Contacts__c, 
                                            (Select Id FROM Contacts)
                                                        FROM Account WHERE Id =:paretAccountIds])
    {        
        I = ParentAccount.Contacts.size();
        List<Contact> allAssociatedConsForChildAccounts = [Select Id FROM Contact WHERE Account.ParentId =:paretAccountIds];
        Y = allAssociatedConsForChildAccounts.size();
        ParentAccount.Total_Contacts__c = I + Y;
        ParentAccountListToUpdate.add(ParentAccount);
    }
   
   
    try
    {
        If(!AccountListToUpdate.IsEmpty())
        {
            Update AccountListToUpdate;
        }
        
        If(!ParentAccountListToUpdate.IsEmpty())
        {
            update ParentAccountListToUpdate;
        }     
    }
    
    Catch(Exception e)
    {
        System.Debug('Exception Thrown: ' + e.getMessage());
    }
}
Hope this helps!