Update Left Employee Count on Deletion

Employee turnover tracking is a vital metric for many organizations—especially those in staffing, HR services, consulting, or managed workforce environments. In Salesforce, it’s common to associate individual Employee records with a parent Account. But without automation, keeping summary metrics like Left Employee Count updated manually can be error-prone and inconsistent.

In this blog, we explore a practical Apex trigger that automatically updates the Left Employee Count field on the related Account whenever an Employee record is deleted. This ensures your reporting remains accurate, your dashboards reflect the latest headcount changes, and your teams never lose visibility into workforce movement.

This trigger runs in the after delete context, capturing Employee deletions and updating the associated Account records accordingly.

đź§  Why This Trigger Matters


Let’s face it—data cleanup and employee lifecycle tracking can easily fall through the cracks if they rely on manual updates. Whether an employee is terminated, transferred, or exits the organization, it’s essential that the parent Account reflects the change in real-time.

Without automation:

  • Reports become unreliable

  • Workforce analytics are inaccurate

  • Sales or service teams might engage with outdated assumptions

  • Stakeholders lose visibility into account stability or risk

With this trigger in place:

  • The Left Employee Count on each Account is kept up to date

  • You avoid manual edits and data sync issues

  • You gain a real-time, accurate reflection of staff turnover

  • Internal dashboards and executive reports remain reliable

This is the kind of automation that brings long-term value to CRM data quality.

🔍 What This Blog Covers


  • How to trigger updates on related objects when a record is deleted

  • Why tracking employee exits at the Account level is critical

  • How to aggregate and update summary fields without manual input

  • Structuring logic inside a handler class for modular, testable code

  • Where this type of logic fits into CRM health and operational reporting

This blog highlights the value of simple, elegant automation that enhances data trust and drives better business decisions.

🎯 Real-World Use Cases for This Trigger


  • Staffing companies monitoring client turnover for contract renewals

  • HR service providers measuring employee exits at client sites

  • Customer success teams identifying churn risk from frequent staff changes

  • Sales teams tracking key personnel movement that may impact deals

  • Analytics teams building Account health scores with real-time inputs

This solution is particularly effective for B2B organizations that track workforce dynamics across enterprise clients or service accounts.

👨‍💻 Developer & Admin Tips


The trigger runs during the after delete phase on the custom Employee object. Here’s what it does:

  • Collects all related Account IDs from the deleted Employee records

  • Fetches the current Left Employee Count for each of those Accounts

  • Adds one to the count for each deleted Employee associated with that Account

  • Updates the corresponding Account records with the new totals

The logic is neatly wrapped inside a trigger handler method, promoting clean architecture and easy testing. This ensures:

  • Bulk-safe processing

  • Accurate count updates even when multiple Employee records are deleted at once

  • Minimal use of platform resources

  • Clear code structure for long-term maintenance and scalability

You can customize or extend this logic to:

  • Track other employee lifecycle metrics like “Currently Active”

  • Update additional Account-level roll-up summaries

  • Trigger internal alerts when turnover thresholds are crossed

For best results, test this trigger with:

  • Single and multiple record deletions

  • Deletion from UI, data import, and external integrations

  • Various combinations of Account and Employee associations

This helps ensure consistent behavior across all user actions and automation pathways.

🎥 Visual Demo Included – YouTube Playlist


Want to see this logic in action? Head over to the Salesforce Makes Sense YouTube playlist, where you’ll find:

  • A full walkthrough of this trigger

  • Real-time examples showing how Account fields update after Employee deletion

  • Clear visuals and practical tips for customizing the logic

  • Testing strategies for a clean, production-ready deployment

The playlist brings every scenario to life, making even advanced Apex concepts easy to understand.

Solution:

trigger EmployeeTrigger on Employee__c (After delete)
      { if(Trigger.isDelete){ if(Trigger.isAfter){
                                      EmployeeTriggerHandler.leftEmpCount(Trigger.old);
                           }
              }
}
public class EmployeeTriggerHandler{

                                 public static void leftEmpCount(List oldEmpList){
                         Set<Id> accIds=new Set<Id>();
                         List<Account> accToBeUpdated=new List<Account>();
                         Map<Id,,Account> accIdToAccMap;
                         List<Employee_c> empList= new List<Employee_c>();
                         Map<Id,Decimal> accIdToTotalCount= new Map<Id,Decimal>();
                         for(Employee__c emp:oldEmpList){
                                   if(emp.Account__c!=null){
                                   accIds.add(emp.Account__c); empList.add(emp);
                                        }
                           }
                           if(!accIds.isEmpty()){
                                           accIdToAccMap= new Map([SELECT
                                           Id,Left_Employee_Count__c FROM Account WHERE Id
                                           IN:accIds]);
                           }
                           if(!empList.isEmpty()){ for(Employee__c emp:empList){
                                         if(accIdToAccMap.containsKey(emp.Account__c)){
                                         if(accIdToTotalCount.containsKey(emp.Account__c)){ Decimal
                                        count =
                                                                                                accIdToTotalCount.get(emp.Account__c)+1;
                                                                                        accIdToTotalCount.put(emp.Account__c,count);
                                         }else{ accIdToTotalCount.put(emp.Account__c,accIdToAcc
                             Map.get(emp.Account__c).Left_Employee_Count__c+1);
                                      }
                         }
          }
   }
   for(Id accId:accIdToTotalCount.keySet()){ Account acc= new Account();acc.Id=accId;
                 acc.Left_Employee_Count__c=accIdToTotalCount.get(accId);
                 accToBeUpdated.add(acc);
       }
      if(!accToBeUpdated.isEmpty()){ update
                     accToBeUpdated;
        }
  }
}

Want to Apply As Content Writer?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)