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 — Plan sprints or milestones excluding non-working days
- Order processing — Estimate delivery timelines excluding holidays
- Payroll calculations — Count billable days in a given month
Before You Start
You’ll need:
- A Salesforce Developer Edition org (free) or any sandbox
- Basic familiarity with Setup, creating custom objects, and the Developer Console
- That’s it — no AppExchange package needed
Calculate Business Days in Salesforce Using Apex
Here are the step-by-step guidelines for creating an application that calculates working days in Salesforce using the Salesforce development framework, Apex.
- Log in to your Salesforce account and make sure you are using Salesforce Lightning Experience.
- Next, open Salesforce Setup:
- Click the Gear Icon in the top-right corner.
- Select Setup from the dropdown menu.

Step 1: Create Salesforce Custom Application
Search for “App Manager“ in the “Quick Find” search box and click on it once you find it.

Click on the “New Lightning App” option in the top right corner of the Lightning Experience App Manager Setup.

You are now on the App Details & Branding step. In the App Details section:
- Enter the App Name
- Add a short Description for the application
In the App Branding section, you can optionally:
- Upload an app logo or image
- Select a highlight color for the navigation bar
After completing the details, click Next.
For this example, I entered the App Name as Business Day Calculator. Once I clicked outside the field, Salesforce automatically populated the Developer Name field.

Once you click Next, you will be directed to the “App Options” step. Simply click “Next” without changing the prior settings.

In this step, you are on the “Utility Items (Desktop Only)” page. Again, click “Next” to move to the next step.

After you click on the next, you are in the “Navigation Items“ step. Here, you need to choose the items you want to include in the app and arrange them in the order they appear.
But until now, we haven’t had an item we want to include, so click “Next” and proceed.

You are now on the User Profiles step. Here, select the profiles that should have access to this Lightning App.
- In the Available Profiles section, search for the required profile.
- Select the profile and click the Add (→) arrow button to move it to the Selected Profiles section.
In this example, I searched for and selected the System Administrator profile, then moved it to the Selected Profiles list.
After adding the required profiles, click Save & Finish to complete the Lightning App creation process for the Business Day Calculator application in Salesforce.

With this, we have successfully created the custom Lightning App for the Business Day Calculator.
Next, we need to create the custom objects and fields needed to store employee details, holiday records, and business-day calculations.
In the following steps, we will create these Salesforce custom objects and configure the required fields.
Step 2: Create Salesforce Custom Objects and Tabs
After creating the Lightning App, we need to create objects to store the data. In Salesforce, Objects are similar to database tables, and Tabs provide a user interface for creating and viewing records.
For this business day calculation application, we will create these custom objects with tabs:
- Employee – Stores employee details and business day records
- National Holiday – Stores USA national holidays to exclude from the calculation
The detailed steps are explained below.
Create Custom Object and Tab: Employee
We need an object named “Employee” that stores the information of the employee. Let’s see step by step how to create an object:
Click on the “Object Manager” in the top navigation bar.

Once you are directed to the object manager setup, click the “Create” dropdown and select “Custom Object”.

You are now on the Custom Object Definition page. In the Custom Object Information section, enter the object details.
For this example:
- Enter Employees in the Plural Label field
- Enter Employee in the Label field
- Salesforce will automatically populate the Object Name field

Scroll down to the Object Creation Options section and select the checkbox for Launch New Custom Tab Wizard after saving this custom object.
Keep the remaining settings at their defaults, then click Save.

Once you click on the save button, you are on the “New Custom Object Tab” step. Select the tab style from the “Tab Style” lookup. After that, click the “Next” button.

After you click on next, you move to the “Add to Profiles” step. Simply click on the “Next” button and move to the next step.

Now, you are in the “Add to Custom Apps” step. Choose the custom apps for which the new custom tab will be available. So, without making any changes, I click on the “Save” button.

Create Custom Object and Tab: National Holiday
We also need one more object named “National Holiday” that stores the list of national holidays in the USA. Let’s see step by step how to create an object:
Again, click on the “Object Manager” in the top navigation bar.

Once you click it, you are moved to the object manager setup. Click the “Create” dropdown and choose “Custom Object”.

You are now on the Custom Object Definition page. In the Custom Object Information section, enter the object labels.
For this example:
- Salesforce will automatically populate the Object Name field
- Enter National Holiday in the Label field
- Enter National Holidays in the Plural Label field

Scroll down to the Object Creation Options section and select the checkbox for Launch New Custom Tab Wizard after saving this custom object.
Click Save to create the custom object and continue to the tab creation process.

After clicking Save, the New Custom Object Tab page will open. Here, select the required Tab Style from the lookup options, then click Next.

You will now be on the Add to Profiles step. Click Next to continue to the next step without making any changes.

You are now on the Add to Custom Apps step. Select the custom apps where this new tab should be available.
In this case, keep the default selection and click Save to complete the custom tab creation process.

Step 3: Create Salesforce Custom Fields
After creating the custom objects and tabs, we need to define the data structure.
In Salesforce, Fields represent the columns in a database table and are used to store specific record information.
Per our business requirements, we will now create the necessary fields in the respective objects. The detailed field configuration is explained in the next steps.
Create Custom Fields within the Employee object
We now need to create fields in the Employee object to store employee-related data, such as Name, Month, and Working Days. These fields will help us calculate and store each employee’s working days.
Let’s create them step by step:
If you are not in Object Manager, click on Object Manager from the top navigation bar.

In the “Quick Find” search bar, search for the newly created custom object “Employee”. Once you find it, click on it.

Click on Fields & Relationships from the left-hand menu. On the Fields & Relationships page, click New in the top-right corner to create a new field.

Note: When you create the custom object Employee, Salesforce automatically creates a standard field called Employee Name.
So, you do not need to create a separate field for the employee name again.After clicking New, you will be redirected to the Choose the Field Type page. Here, select the type of data you want to store in the field.
For the Month field, choose Picklist as the data type since it will contain a fixed list of values (months of the year).
Select the Picklist option and click Next.

You are now on the Enter the details step. Enter the Field Label as Month, and the Field Name will be automatically generated by Salesforce.
In the Picklist Values section, define the values for the field.
You have two options:
- Use a Global Value Set, if already created
- Or manually enter values
For this example, select Enter values, with each value separated by a new line, and add month names such as January, February, March, etc.
After adding all values, click Next.

After clicking Next, you will be on the Establish Field-Level Security step.
Here, select the profiles that should have edit access to this field. By default, some profiles may already be selected.
Review the selections and click Next to continue.

You are now on the Add to Page Layouts step. By default, all page layouts are already selected. Keep the default selection as it is.
Click Save & New to save the field and immediately create another custom field

We also need to create one more field called Working Day to store the total number of working days for the selected month.
Since we previously clicked Save & New, Salesforce opens the Choose the Field Type step again.
In this step, select the data type for the field. For Working Day, choose Number because this field stores numeric values (the count of working days).
Select the Number radio button and click Next.

After clicking Next, you will be on the Enter the details step. Enter the Field Label “Working Day,” and Salesforce will automatically populate the Field Name.
Next, configure the number field properties.
- Set Length to 17
- Set Decimal Places to 1
Then click Next.

You are now on the Establish Field-Level Security step. Select the profiles that should have edit access to this field. Some profiles may already be selected by default.
After reviewing the access settings, click Next.

After clicking Next, you will be on the Add to Page Layouts step.
By default, all page layouts are already selected. Keep the default selection and click Save to complete the field creation process.

Create Custom Fields within the National Holiday object
We also need to create fields in the National Holiday object to store holiday-related information such as the holiday name and holiday date.
These fields will help us exclude national holidays while calculating business days. The required fields are:
- National Holiday Name – Stores the name of the holiday
- Date – Stores the holiday date
Let’s create these fields step by step.
Click on “Object Manager” in the top navigation bar if you aren’t already there.

Search for the custom object “National Holiday” in the “Quick Find” search bar, and then click it.

3: On the left side of the page, select “Fields & Relationships”. Once you are on the fields and relationship page setup, click the “New” button in the upper right corner.

Note: When you create the National Holiday custom object, Salesforce automatically creates a standard field called National Holiday Name.
So, there is no need to create this field again.After clicking New, you will be redirected to the Choose the Field Type step. Here, select the type of data you want to store in the field.
Since this field will store the holiday date, choose Date as the data type by selecting the radio button next to it. Then click Next.

You are now on the Enter the details step. Enter the Field Label as Date, and Salesforce will automatically populate the Field Name.
After that, click Next.

After clicking Next, you will be on the Establish Field-Level Security step. Select the profiles that should have edit access to this field. Some profiles may already be selected by default.
Keep the default selection and click Next.

You are now on the Add to Page Layouts step.
By default, all page layouts are already selected. Keep the default selection and click Save to complete the field creation process in the National Holiday object.

With this, we have successfully created the required custom fields, objects, and tabs in Salesforce Lightning.
Next, we need to add these custom object tabs to the Business Day Calculator Lightning App so users can access them directly from the application navigation bar.
Step 4: Add Salesforce Custom Tabs to App
To make the application easier to use, we also need to add the custom object tabs to the Salesforce Lightning App.
This allows users to quickly access and manage application records. Let’s see how to add custom tabs to the Salesforce app.
Click the Gear Icon in the top-right corner and select Setup from the dropdown menu.

On the left-hand page, in the “Quick Find” box, search for “App Manager,” then click it.

After opening the Apps page, you will see the list of all Salesforce apps, including standard and custom apps. Search for the Business Day Calculator app that we created earlier.
Click the dropdown arrow next to the app name and select Edit to modify the application settings.

In the App Settings section, select Navigation Items. From the Available Items list, select the tabs you want to add to the app and move them to the Selected Items list using the arrow buttons.
For this example, add:
- Employee
- National Holiday
You can also arrange the tabs in the order you want them to appear in the app navigation bar. After adding the required tabs, click Save to complete the process.

With this, we have successfully added the custom tabs of the objects to the application navigation bar. Now, we need to create the Salesforce Apex Class in the next step.
Step 5: Create Salesforce Apex Class
Next, we need to create an Apex Class to define the custom business logic for calculating business days by excluding Saturdays, Sundays, and National Holidays.
Let’s create the Apex Class step by step. Go back to the Salesforce Setup page, search for Apex Classes in the Quick Find box, and click on it.

After clicking Apex Classes, the Apex Classes page will open. This page allows you to create and manage Apex code in Salesforce.
Apex is Salesforce’s object-oriented programming language for implementing custom business logic on the Lightning Platform.
Now, click on the Developer Console button to write the Apex class code.

After clicking Developer Console, a new Developer Console window will open.
Click the File menu from the top navigation bar. From the dropdown menu, select New and then click Apex Class.

A New Apex Class pop-up window will appear. Enter the name of the Apex Class and click OK.
For this example, enter the class name as BusinessDayCalculation.

After clicking OK, the Apex Class editor will open in the Developer Console. This is where you can write the Apex code for your business logic.
Enter the Apex code for calculating business days by excluding Saturdays, Sundays, and USA National Holidays for the selected employee and month.
Below is the Apex class code used for this business day calculation.
public class BusinessDayCalculation {
public static void calculateAndUpdateWorkingDays(List<Employee__c> employees) {
Map<Integer, Set<Date>> holidaysMap = new Map<Integer, Set<Date>>();
for (National_Holiday__c currHoliday : [SELECT Date__c FROM National_Holiday__c]) {
Integer holidayMonth = currHoliday.Date__c.month();
if (!holidaysMap.containsKey(holidayMonth)) {
holidaysMap.put(holidayMonth, new Set<Date>());
}
holidaysMap.get(holidayMonth).add(currHoliday.Date__c);
}
for (Employee__c emp : employees) {
String selectedMonth = emp.Month__c;
if (selectedMonth != null) {
Integer year = Date.today().year(); // Assuming the year is not needed in the picklist
Integer month = getMonthNumber(selectedMonth);
Date startDate = Date.newInstance(year, month, 1);
Date endDate = startDate.addMonths(1).addDays(-1);
Set<Date> holidaysSet = holidaysMap.containsKey(startDate.month()) ?
holidaysMap.get(startDate.month()) : new Set<Date>();
Integer workingDays = 0;
for (Date dt = startDate; dt <= endDate; dt = dt.addDays(1)) {
DateTime currDate = DateTime.newInstance(dt.year(), dt.month(), dt.day());
String todayDay = currDate.format('EEEE');
if (todayDay != 'Saturday' && todayDay != 'Sunday' && !holidaysSet.contains(dt)) {
workingDays++;
}
}
// Update the field without DML
emp.Working_Day__c = workingDays;
}
}
}
public static Integer getMonthNumber(String monthName) {
monthName = monthName.toLowerCase();
Map<String, Integer> monthMap = new Map<String, Integer>{
'january' => 1, 'february' => 2, 'march' => 3, 'april' => 4, 'may' => 5, 'june' => 6,
'july' => 7, 'august' => 8, 'september' => 9, 'october' => 10, 'november' => 11, 'december' => 12
};
return monthMap.containsKey(monthName) ? monthMap.get(monthName) : 1; // Default to January if not found
}
}A line-by-line explanation of the code:
- We created an Apex class named BusinessDayCalculation with a method called calculateAndUpdateWorkingDays that accepts Employee__c records.
- The holidaysMap map stores holiday dates month-wise from the National_Holiday__c object.
- The code loops through each employee record and gets the selected month from the Month__c field.
- It calculates the first and last dates of the selected month, then checks each day in turn.
- Saturdays, Sundays, and National Holidays are skipped while counting the working days.
- The final business day count is stored in the Working_Day__c field.
- A helper method named getMonthNumber converts the month name into its numeric value.

8: After entering the code, click on the “File” option in the top navigation, and from here, click on the “Save” button to successfully save the code of the apex class.

With this, we have successfully created an Apex code in Salesforce to calculate the working days. Now, we need to create the Salesforce Apex Trigger in the next step.
Step 6: Create Salesforce Apex Trigger
We also need to create an Apex Trigger to automatically calculate business days when Employee records are created or updated in Salesforce.
Let’s create the Apex Trigger step by step.
- In the Developer Console, click the File menu from the top navigation bar.
- Select New and then click Apex Trigger

A New Apex Trigger pop-up window will appear.
Enter the trigger name in the Name field and select the related object from the sObject dropdown. For this example:
- Trigger Name: EmployeeTrigger
- sObject: Employee__c
This trigger will run whenever Employee records are created, updated, or deleted. Click Submit to open the trigger editor in the Developer Console, where you can add the Apex Trigger code.

Enter the code to create an Apex trigger. Below is the code I created to perform actions on the creation, update, or deletion of an employee record.
trigger EmployeeTrigger on Employee__c (before insert, before update) {
List<Employee__c> employeesToUpdate = new List<Employee__c>();
for (Employee__c emp : Trigger.new) {
if (Trigger.isInsert || (Trigger.isUpdate && emp.Month__c != Trigger.oldMap.get(emp.Id).Month__c)) {
employeesToUpdate.add(emp);
}
}
if (!employeesToUpdate.isEmpty()) {
BusinessDayCalculation.calculateAndUpdateWorkingDays(employeesToUpdate);
}
}A line-by-line explanation of the code:
- We created an Apex Trigger named EmployeeTrigger on the Employee__c object. The trigger runs before records are inserted or updated.
- The employeesToUpdate list stores Employee records that require business-day calculation.
- The trigger loops through records available in Trigger.new, which contains the new versions of inserted or updated records.
- An if condition checks:
- If the record is newly inserted, orIf the Month__c field value has changed during update
- Finally, if the list contains records, the trigger calls the calculateAndUpdateWorkingDays method in the BusinessDayCalculation class to automatically calculate and update the working days.

After entering the code, select “File” from the top navigation, and then click the “Save” button to complete the saving process of the Apex trigger code.

With this, we have successfully added the Salesforce Apex Trigger code and completed the business-day calculation process in Salesforce. Now, we need to check how your application works.
Step 7: Test Salesforce Calculate Working Day Application
After completing the Salesforce application for calculating business days by excluding weekends and USA National Holidays, it’s time to test the application and verify the results.
Follow these steps to test the application:
- Click the Gear Icon in the top-right corner and select Setup.
- Open the App Launcher, search for Business Day Calculator, and click on the app to open it.

Insert National Holiday Records
Open the National Holidays tab from the navigation bar. Click the New button to create a new national holiday record. In the New National Holiday pop-up window:
- Enter the National Holiday Name
- Select the holiday Date
Click Save & New to save the record and add another holiday record.

Note: Repeat the Save & New process until all required USA National Holidays are added. With this, we have successfully added records to the National Holiday object. Next, we will add records to the Employee object to test the business day calculation functionality.
Insert Employee Records
- Open the Employees tab from the navigation bar.
- Click the New button to create a new employee record.
- In the New Employee pop-up window:
- Enter the Employee Name
- Select the Month for which you want to calculate the working days
Note: Leave the Working Day field blank. It will be automatically populated when the record is saved.- Click Save to save the employee record.

As a result, we have learned to add a record to the Salesforce Employee object. Now, let’s check the employee’s result for the working day.
Proof of Concept
Follow these steps to verify the business day calculation result:
- Open any employee record from the Employees list.
For this example, select David Daniel. - On the employee details page, you can see that the Working Day field is automatically calculated for the selected month, March.
The calculated value is 23.0 working days.
Now let’s verify the result:
- March 2023 has 31 total days
- It contains:
- 4 Saturdays
- 4 Sundays
- 0 USA National Holidays
So, the total non-working days are: 4 + 4 =8
Now subtract the non-working days from the total days in March: 31−8=23
This confirms that the Apex code successfully calculated the correct number of business days.

Now, update the employee record by clicking the Edit (Pencil) Icon, changing the Month value, and saving the record.
For this example, change the month from March to July. After saving the record, the Working Day field is automatically updated to 20.0.
Let’s verify the result:
- July has 31 total days
- It contains:
- 5 Saturdays
- 5 Sundays
- 1 USA National Holiday
So, the total non-working days are: 5 + 5+ 1 =11
Now subtract the non-working days from the total days in July: 31−11=20
This confirms that the Apex code is correctly calculating business days after record updates as well.

With this, we have successfully created and tested a Salesforce application that calculates employee business days for a selected month, excluding weekends and USA National Holidays, using Apex.
This application automatically calculates and updates working days whenever employee records are created or updated, ensuring a fully automated process in Salesforce.
Conclusion
With this, we have learned to create a Salesforce application using the Salesforce developer framework language Apex to calculate the business days.
We also learned the process of creating custom objects with fields in a custom application, an apex class, trigger coding to calculate the working days in a specified month, and last but not least to apply this concept within a practical example.
You may like to read the following topics:
- Salesforce Date Formula Business Days
- Salesforce Last Activity Date Formula
- Calculate Business Days Using Salesforce Flow
- Add a Custom Navigation Menu in Salesforce Lightning Experience
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.