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
Jenna HildebrandJenna Hildebrand 

Apex to Count Total Employees in Account Hierarchy

Hello,

I need to write an apex class that sums the Employees field on all accounts within a hierarchy and updates a custom field called "Total Hierarchy Employees" with the result on each account. For example:
  • Test Account 1 – 10 employees
    • Test Account 2 – 15 employees
    • Test Account 3 – 12 employees
The "Total Hierarchy Employees" field should read "37" on Test Account 1, 2, and 3.

I've gotten my apex class started, but I'm having trouble avoiding nesting loops and such. Any help is greatly appreciated.

Thank you!
itzmukeshy7itzmukeshy7
It would be great if you can share what you have done so far.
Jenna HildebrandJenna Hildebrand
So far, I've created a map of Ultimate Parent Accounts and their Child Accounts (i.e., account hierarchies). Now, for each Ultimate Parent Account, I need to loop through the Child Accounts and get the sum of their Employees. Then, I will update the Total Hierarchy Employees field with that sum for each account in the hierarchy.

One important item to note—I'm not using the standard Parent Account field because I need the logic to be able to handle multiple tiers as well as include accounts that do not have a parent (for example, accounts that are not part of a hierarchy are their own Ultimate Parent Accounts). Also, I can't use AggregateResults because the field I'd be grouping by is a formula field.

Appreciate any assistance here!
public class AccountController {

    public static void countTotalHierarchyEmployees(Account[] accounts) {
    
        List<String> ultimateParentAccountIDs = new List<String>();
        
        for(Account a : accounts) {
        
            if(!ultimateParentAccountIDs.contains(a.Ultimate_Parent_Account_ID__c)) {
                ultimateParentAccountIDs.add(a.Ultimate_Parent_Account_ID__c);
            }
        }
        
        System.debug('ultimateParentAccountIDs List ---> ' + ultimateParentAccountIDs);
        
        List<Account> childrenAccts = [SELECT Id, NumberOfEmployees, Ultimate_Parent_Account_ID__c FROM Account
                                       WHERE Ultimate_Parent_Account_ID__c IN : ultimateParentAccountIDs];
    
        Map<Id, List<Id>> parentToChildrenMap = new Map<Id, List<Id>>();
        
        for(Account a : childrenAccts) {
            if(parentToChildrenMap.containsKey(a.Ultimate_Parent_Account_ID__c)) {
                List<Id> childrenIDs = parentToChildrenMap.get(a.Ultimate_Parent_Account_ID__c);
                childrenIDs.add(a.Id);
                parentToChildrenMap.put(a.Ultimate_Parent_Account_ID__c, childrenIDs);
            }
            else {
                parentToChildrenMap.put(a.Ultimate_Parent_Account_ID__c, new List<Id> { a.Id });
            }
        }
        
        System.debug('parentToChildrenMap ---> ' + parentToChildrenMap);
    }
}