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
Hemant Kumar 119Hemant Kumar 119 

How to update no of childcount(Field) in parentaccount detailpage its fallow accountHierarchy up to three levals using Trigger.

How to update no child count(Field) in parent account detail page its follow account hierarchy up to three levels using Trigger.

trigger NumberOfChieldCount on Account (after Insert, after Update, after delete) {
    Set<id> ids= new Set<id>();
    List<Account> acclist = new List<Account>();
    integer count = 0;
   if(Trigger.isInsert || Trigger.isUpdate){
        for(Account acc: Trigger.new){
            if(acc.ParentId!=null)
                ids.add(acc.ParentId);
            acclist.add(acc);
        }
    }
    
    if(Trigger.isDelete){
        for(Account acc: Trigger.old){
            if(acc.ParentId!=null)
                ids.add(acc.ParentId);
            acclist.add(acc);
        }
    }   
    if(ids.size()>0){
        List<Account> accchild = new List<Account>([select id,Parentid from Account where Parentid IN: ids]);
        List<Account> accparent = new List<Account>([select id,Nubmer_of_Chields__c from Account where id IN: ids]);
        /*integer nochilds=[select count() from Account where Parentid in :ids];
        system.debug('nochilds::::::::'+nochilds);
        integer noofchilds=[select count() from Account where id in :ids];
        system.debug('noofchilds::::::::'+noofchilds);
        system.debug('accchild 27-->'+accchild);
        system.debug('accparent 28-->'+accparent);
        integer noof;
        integer nofchids;
        integer nc;*/
        for(Account ac: accparent){
            count =0;
            for(Account acchilds: accchild){
                /*if(acchilds.Nubmer_of_Chields__c!=null && ac.Nubmer_of_Chields__c!=null)
                    nofchids=integer.valueof(acchilds.Nubmer_of_Chields__c)+integer.valueof(ac.Nubmer_of_Chields__c);
                if(acchilds.Nubmer_of_Chields__c!=null && ac.Nubmer_of_Chields__c==null)
                    nofchids=integer.valueof(acchilds.Nubmer_of_Chields__c);   
                
                system.debug('noof:::: 36'+nofchids );*/
                if(acchilds.Parentid == ac.id)
                    count++;
                   else
                    count--;
            }
            //ac.Nubmer_of_Chields__c =string.valueof(count); 
            /*system.debug('ac.Nubmer_of_Chields__c::::::::'+ac.Nubmer_of_Chields__c);
            system.debug('count:::: 47'+count);
            system.debug('count:::: 47'+nofchids);*/
            //if(count>0 && nofchids!=null)
                //ac.Nubmer_of_Chields__c =string.valueof(count+nofchids); 
            //if(count>0 && nofchids==null) 
               //if(ac.Parentid == ac.id)
                ac.Nubmer_of_Chields__c =string.valueof(count);
               
                    
            /*else
                ac.Nubmer_of_Chields__c =string.valueof(count);  */         

        }
        
        try{
            upsert accparent;
        }catch(DMLException ex){
            System.debug('Exception is '+ex);
        }
    }
}
Note:Create  new field Nubmer_of_Chields__c in Account(Object).

A-->No of child are 2+1 =3
   B--->2
      c---->1
 
SFDC Apex DevSFDC Apex Dev
Hi Hemant,

Try below code.
trigger NumberOfChieldCount on Account (after Insert, after update, after delete) {
    set<id> accid = new set<id>();
	list<account> accountlist = new list<account>();
	list<account> listacc = new list<account>();
	list<account> parentAcclist = new list<account>();
	list<account> listAccParent = new list<account>();
	map<id,integer> mapCount = new map<id,integer>();
   if(Trigger.isInsert){
        for(Account acc: Trigger.new){
            if(acc.ParentId!=null){
                accid.add(acc.ParentId);
			}
        }
    }
    
    if(Trigger.isDelete || Trigger.isUpdate){
        for(Account acc: Trigger.old){
            if(acc.ParentId!=null){
                accid.add(acc.ParentId);
			}
        }
    }
	parentAcclist = “select id, name from account where id in:accid”;
	accountlist = “select id, name, ParentId, Nubmer_of_Chields__c from account where ParentId in:accid”;
	for(Account acc: parentAcclist){
		listacc.clear;
		for(Account childAcc: accountlist){
			if(childAcc.ParentId == acc.Id){
				listacc.add(childAcc);
				mapCount.put(childAcc.ParentId, listacc.size());
			}
		}
	}
	if(parentAcclist.size()>0){
		for(Account acct: parentAcclist){
			if(mapCount.get(acct.Id) == null){
				acct.Nubmer_of_Chields__c = 0;
			}
			else{
				acct.Nubmer_of_Chields__c = mapCount.get(acct.id);
				listAccParent.add(acct);
			}
		}
	}
	if(listAccParent.size()>0){
		update listAccParent;
	}
}

Mark this as the best answer if this resolve your issue.

Thanks!
Chirag
Hemant Kumar 119Hemant Kumar 119
Hi Chirag,
I have tried with the above code, is not updating, could you please update the code.

Thanks!
Hemant Kumar 119Hemant Kumar 119
Hi Chirag,
I have tried with the above code, is not updating, could you please update the code.

Note: only one level parent account child account records got it in debug, UI is not working, But child account to parent accounts to parent. the parent accounts not updating/found in debug/UI.

Thanks!
Hemant