How to Calculate Business Days in Salesforce Using Apex (Exclude Weekends & Holidays)

If you have ever tried to calculate business days in Salesforce by simply subtracting two dates, you probably noticed one problem: Salesforce returns calendar days, not working days. That means the result also includes Saturdays, Sundays, and public holidays.

But in real business scenarios, such as SLA tracking, employee leave management, or order processing timelines, we usually need only working days.

In this tutorial, I will show you how to build a complete Salesforce Lightning solution using Apex that can:

  • Calculate the total business days for an employee in a selected month
  • Automatically exclude weekends (Saturday and Sunday)
  • Exclude US National Holidays stored in a custom Salesforce object
  • Run the calculation automatically using an Apex Trigger whenever records are created or updated — no manual action required

By the end of this tutorial, you will have a fully working Salesforce app that uses custom objects, Apex classes, and triggers to automatically calculate accurate business days.

Understand Salesforce Business Day Calculation Scenario

This application will automatically calculate the total number of working days in a selected month, excluding weekends and national holidays.

For better understanding, we will use an Employee example. The application will calculate the business days for each employee based on the selected month.

The calculation process works like this:

  • Start with the total number of days in the selected month
  • Exclude Saturdays and Sundays
  • Exclude USA National Holidays stored in Salesforce
  • Return the final number of business days (working days)

This is a practical, real-time use case for employee management, leave tracking, payroll systems, SLA calculations, and many other Salesforce business processes.

What We’re Building

Here’s a quick picture of what the app does:

  • You have an Employee custom object with fields for the employee’s name and the month you want to calculate for.
  • You have a National Holiday custom object that stores all public holiday dates.
  • When you save an employee record and select a month, Salesforce automatically calculates how many working days are in that month and stores the result in a field called Working Days.

This kind of setup is useful for:

  • Leave management — Auto-calculate approved working days for leave requests
  • SLA tracking — Count remaining business days before a Case deadline
  • Project scheduling&nbs