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
Shreya SalesforceShreya Salesforce 

Trigger on parent Account to get sum of all child Accounts. I make a custom field 'Total_No_of_Child_Accounts__c'(Number type)on Account Object.

I make Acc1(child object)and Acc2(child object) whose parent is 'Acc3'. My trigger shows only '1'in field 'Total_No_of_Child_Accounts__c'.
Please help me to solve this problem.

trigger TotalChildAccounts on Account (after insert,after Update,after delete,after Undelete) 
{
    //List<Account> childaccIds= new List<Account>();
    //List<Id> ParentaccIds= new List<Id>();
    Map<Id,Account> accMap= new Map<Id,Account>();
    if(trigger.isInsert || trigger.isUpdate || trigger.isUndelete)
    {
        for(Account acc:trigger.new)
        {
            if(acc.ParentId!=null)
            {
                accMap.put(acc.ParentId,acc);
                //childaccIds.add(acc);
                //ParentaccIds.add(acc.ParentId);
                // System.debug('childaccIds=='+childaccIds);
                // System.debug('ParentaccIds=='+ParentaccIds);
            }
        }
    }
    if(trigger.isDelete)
    {
        for(Account acc:trigger.old)
        {
            if(acc.ParentId!=null)
            {
                accMap.put(acc.ParentId,acc);
            }
        }
    }
    List<Account> FetchParentAccList = [SELECT Id,Name,Total_No_of_Child_Accounts__c,ParentId FROM Account WHERE ParentId IN:accMap.keyset()];
    List<Account> accList = new List<Account>();
        For(Account achild : FetchParentAccList)
        {
            if(accMap.containskey(achild.Id))
            {
                achild.Total_No_of_Child_Accounts__c = FetchParentAccList.size();
                accList.add(achild);
            }
            
            
        }
        if(accList.size()>0)
        {
        Update acclist;
        }
}
Maharajan CMaharajan C
HI Shreya,

Try the below trigger:


trigger CountChildAccount on Account(after insert, after update, after delete) {
    Set<Id> Ids= new Set<Id>();

    if(Trigger.isInsert || Trigger.isUpdate){
        for(Account acc: Trigger.new){
            if(acc.ParentId!=null){
                Ids.add(acc.ParentId);
            }
        }
    }

    if(Trigger.isDelete){
       for(Account acc: Trigger.old){
            if(acc.ParentId!=null){
                Ids.add(acc.ParentId);
            }
        }
    }
    
    if (!Ids.isEmpty()) {
        List<Account> AccountToUpdate = new List<Account>();
                
        for (AggregateResult ar : [SELECT COUNT(ID), ParentID FROM Account 
                                WHERE ParentID IN :Ids GROUP BY ParentID]) {
            Id accID = (ID)ar.get('ParentID');
            Integer count = (Integer)ar.get('expr0');
            Account acc1 = new Account(Id=accID);
            acc1.Total_No_of_Child_Accounts__c = count;
            AccountToUpdate.add(acc1);
        }

        if (!AccountToUpdate.isEmpty()) {
            update AccountToUpdate;
        }
    }
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C