Track Present Employee Count on Events

Maintaining accurate workforce data is a foundational part of running a clean, insights-driven Salesforce org. Whether you’re managing staffing across projects, monitoring employee engagement, or tracking account-level activity, knowing exactly how many employees are linked to a particular Account is crucial.

In this blog, we break down a powerful and practical Apex trigger that automatically updates the “Present Employee Count” field on the related Account whenever Employee records are inserted, deleted, or undeleted. It ensures that your employee headcount is always up to date—no spreadsheets, no manual edits, just real-time accuracy built right into your Salesforce data model.

This trigger runs in the after insert, after delete, and after undelete contexts, capturing all changes to the Employee object and updating the parent Account accordingly.

🧠 Why This Trigger Is So Valuable


Think about the number of ways employee records can change:

  • A new hire is added

  • An employee is marked inactive or deleted

  • An employee is reinstated (undeleted) after being re-hired

If your org relies on manual updates to track how many employees are currently associated with each Account, that number can quickly fall out of sync. This leads to:

  • Misleading reporting on staffing levels

  • Incorrect resource allocation

  • Confusion during audits or client meetings

  • Broken downstream logic relying on accurate counts

With this trigger:

  • Your Present Employee Count field updates automatically

  • You eliminate the need for manual field edits

  • All changes—insert, delete, and undelete—are handled in one seamless automation

  • Every Account reflects the real-time status of its linked workforce

This simple trigger helps keep your data trustworthy and your processes smart.

🔍 What This Blog Covers


  • How to capture insert, delete, and undelete events in a single trigger

  • How to keep a summary field (Present Employee Count) in sync with related records

  • Why a clean trigger handler method improves clarity and maintainability

  • How this trigger supports broader HR, resourcing, and client management needs

  • Where this logic fits into real-world CRM and operational processes

This is one of those automations that does one job—and does it exceptionally well.

🎯 Real-World Use Cases for This Trigger


  • Staffing agencies needing to report headcount per client

  • HR and onboarding teams who track active employees by department or region

  • Consulting firms assigning consultants to client accounts

  • Customer success teams assessing account complexity or engagement

  • Sales teams analyzing account size to inform upsell or renewal strategies

Anywhere there’s a relationship between Accounts and Employees, this automation ensures you’re never working with outdated numbers.

👨‍💻 Developer & Admin Tips


Here’s what this trigger does:

  • It listens to insert, delete, and undelete events on the Employee object

  • It gathers the IDs of all related Accounts from the affected Employee records

  • It loops through each Account and recalculates the number of linked Employees

  • It updates the Present Employee Count field with the new total

  • It performs all changes in a bulk-safe, optimized manner using a single update call

The logic is housed inside a handler method, making it easy to:

  • Maintain and test

  • Extend or reuse in the future

  • Keep your trigger clean and focused

You can easily expand this pattern to support:

  • Filtering only “active” employees

  • Tracking employee counts by region or department

  • Sending alerts when thresholds are crossed (e.g., 100+ employees on an account)

  • Integrating this logic with reports or dashboard metrics

Make sure to test this trigger with:

  • Single and multiple record insertions

  • Deletions through UI, list views, or automation

  • Undelete actions using the Recycle Bin or API

It’s also good practice to verify that counts update immediately in reports or on custom Lightning record pages.

🎥 Hands-On Demo Available – YouTube Playlist


To help you implement this step-by-step, check out the Salesforce Makes Sense YouTube playlist. In the demo, you’ll learn:

  • How to trigger logic across multiple events

  • How to recalculate related record summaries

  • How to test and verify the behavior for all scenarios

  • Admin tips for extending the solution to custom use cases

The playlist is built with practical use in mind, making it easy for developers, admins, and curious learners to follow and apply.

Solution:

//Same code will work for insert, delete and undelete trigger EmployeeTrigger
on Employee__c(after insert,after delete,after undelete) { if(Trigger.isInsert){ if(Trigger.isAfter){
                                                          EmployeeTriggerHandler.updatePresentEmpCount(Trigger.New);
                       }
        }
        if(Trigger.isDelete){ if(Trigger.isAfter){
                                                          EmployeeTriggerHandler.updatePresentEmpCount(Trigger.New);
                      }
      }
         if(Trigger.isUnDelete){ if(Trigger.isAfter){
                                                           EmployeeTriggerHandler.updatePresentEmpCount(Trigger.New);
                       }
        }
}
public class EmployeeTriggerHandler{

public static void updatePresentEmpCount(List<Employee_c> empList){
    List<Account> accList= new List<Account>(); Set<Id> idSet= new
    Set<Id>();
    for(Employee__c emp:empList){ if(emp.Account__c!=null){
    idSet.add(emp.Account__c);
                   }
   }
   for(Account acc:[SELECT Id, Name,(SELECT Id FROM Employees__r)
                   FROM Account WHERE Id IN:idSet]){
                   acc.Present_Employee_Count__c=acc.Employees__r.size();
                   accList.add(acc);
}
if(!accList.isEmpty()){ update accList;
}
}

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 :)