Calculate Working Days Using Apex In SalesForce

Introduction 
 
Here, in this blog, we will learn how to exclude holidays (stored in the custom object) and weekends while calculating the number of working days between two given dates.
 
This is kind of a quick tutorial. I was looking for this solution on different forums but couldn't get a proper solution. That's why I have implemented a simple business logic for that. 
 
First, we need to create a custom object which is used to store the details of holidays.
 
Calculate Working Days Using Apex In SalesForce
 
 Calculate Working Days Using Apex In SalesForce
 
Then, let us create an apex class which returns the value of the working days. In this class, we need to create a static method which has two date type parameters; and then, we have to fetch the dates from a custom object using SOQL (Salesforce Object Query Language).
  1. public class BusinessDayCalculation  
  2. {  
  3.     public static Integer calculateWorkingDays(Date startDate, Date endDate)  
  4.     {          
  5.          
  6.       
  7.         Set<Date> holidaysSet = new Set<Date>();  
  8.          
  9.         for(Holiday__c currHoliday : [Select Date__c from Holiday__c])  
  10.         {  
  11.             holidaysSet.add(currHoliday.Date__c);  
  12.         }  
  13.          
  14.         Integer workingDays = 0;  
  15.          
  16.         for(integer i=0; i <= startDate.daysBetween(endDate); i++)  
  17.         {  
  18.             Date dt = startDate + i;  
  19.             DateTime currDate = DateTime.newInstance(dt.year(), dt.month(), dt.day());  
  20.             String todayDay = currDate.format('EEEE');  
  21.             if(todayDay != 'Saturday' && todayDay !='Sunday' && (!holidaysSet.contains(dt)))  
  22.                 {  
  23.                     workingDays = workingDays + 1;  
  24.                 }     
  25.                
  26.         }  
  27.               
  28.         System.debug('--Working days'+workingDays);  
  29.         return workingDays;  
  30.     }  
  31. }  
 After that, we have to create one custom field to store the working days.
 
Calculate Working Days Using Apex In SalesForce
 
For storing the working days in the created custom field, we have to create an apex trigger that updates this custom field with working days. Then, it is returned by an apex class.
  1. trigger LeaveRequestTrigger on Leave_Request__c (before insert, before update)   
  2. {  
  3.     for(Leave_Request__c LR : Trigger.New)  
  4.     {  
  5.          
  6.         Integer workingDays = BusinessDayCalculation.calculateWorkingDays(LR.First_Day_Off__c,LR.Last_Day_Off__c);  
  7.              
  8.         if(LR.Half_Day_AM__c == TRUE && LR.Half_Day_Pm__c == TRUE)  
  9.         {  
  10.             LR.Working_Days__c = workingDays - 1;   
  11.         }  
  12.              
  13.         else if(LR.Half_Day_AM__c == TRUE || LR.Half_Day_Pm__c == TRUE )  
  14.         {    
  15.            LR.Working_Days__c = workingDays - 0.5;             
  16.         }  
  17.               
  18.         else   
  19.         {  
  20.           LR.Working_Days__c = workingDays;          
  21.         }  
  22.     }  
  23. }  
Conclusion
 
Here, in this quick tutorial, we learned how to calculate business working days between two dates excluding holidays and weekends. I hope this guide will help you to calculate such important stuff by using custom business logic.
 
Thanks for reading.