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
Nagma KhanNagma Khan 

how to count the child account related to parent Account on the Parent Account?

hi


Please how to count the child account Related to the parent Account on the Parent Account? used with the Trigger


Thanks
Nagma Khan
Deepak Maheshwari 7Deepak Maheshwari 7

Hi Nagma,

 

Please create a Count__c field on Account of Number data type.And use below trigger:

 

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

    List<Account> acclist = new List<Account>();

 
    
    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.isEmpty()) {
        List<Account> AccountToUpdate = new List<Account>();
        
        Map<Id, Integer> mapcount = new Map<Id, Integer>();
        
        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.Count__c= count;
            AccountToUpdate.add(acc1);
        }
        
        

        if (!AccountToUpdate.isEmpty()) {
            update AccountToUpdate;
        }
    }
}
Shivdeep KumarShivdeep Kumar
Hi Nagma,

Please update the below code, This code is working fine for Account and child contacts !
 
trigger CountchildRecordsonParent on Contact(after insert, after update, after delete, after undelete) {
   List<Account> accounts = new List<Account>();   
   Set<Id> custord = new Set<Id>();
   
   if(Trigger.isDelete) {
     for(Contact con:Trigger.Old) {      
        custord.add(con.AccountId);    
     }      
   }else if(Trigger.isUpdate) {
     for(Contact con:Trigger.New) {      
        custord.add(con.AccountId);     
     }
     for(Contact con:Trigger.Old) {      
        custord.add(con.AccountId);   
     }      
   }else {
     for(Contact con:Trigger.New) {      
        custord.add(con.AccountId);     
     }
   }
   
   AggregateResult[] groupedResults = [SELECT COUNT(Id), AccountId FROM Contact where AccountId IN :custord GROUP BY AccountId ];
   
   for(AggregateResult ar:groupedResults) {     
     Id custid = (ID)ar.get('AccountId');     
     Integer count = (INTEGER)ar.get('expr0');     
     Account cust1 = new Account(Id=custid);     
     cust1.Total_No_of_Child__c = count;     
     accounts.add(cust1);      
   }   
   update accounts;
}

Please let me know , if this help !

Thanks
Shivdeep​
 
Deepak Maheshwari 7Deepak Maheshwari 7

Hi Nagma,

 

Hope the Trigger worked for you!
If not please let me know.

 

Thanks
Deepak