function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
dellseysdellseys 

Check if next day is a holiday in salesforce

Hi,

I am developing a trigger to check if nexday =today()+1  is a holiday and if it is, it should take the next business day that is not a holiday. How can i do this in salesforce.

thanks
steve
Raj VakatiRaj Vakati
First , You need to maintain holidays in Salesforce

Your Name | Setup | Company Profile | Holidays.
Click Edit to edit an existing holiday.
Click Del to delete an existing holiday.
You can only delete a holiday that is not associated with any business hours.
Click Clone next to an elapsed holiday to clone it.
The Elapsed Holidays related list displays holidays that have occurred in the past. You can only clone elapsed holidays.



Second, You can able to query holidays  and compare in the code 



 
public Integer calculateWorkingDaysBetweenTwoDates(Date date1,Date date2){

               List<Holiday> holidays=[Select h.StartTimeInMinutes, h.Name, h.ActivityDate From Holiday h];

                Integer allDaysBetween = date1.daysBetween(date2);
                Integer allWorkingDays=0;
                for(Integer k=0;k<allDaysBetween ;k++ ){
                    if(checkifItisWorkingDay(date1.addDays(k),holidays)){
                        allWorkingDays++;
                    } 
                }

                return allWorkingDays;

      }

public boolean checkifItisWorkingDay(Date currentDate,List<Holiday> holidays){
                 Date weekStart  = currentDate.toStartofWeek();
                for(Holiday hDay:holidays){
                        if(currentDate.daysBetween(hDay.ActivityDate) == 0){
                                 return false;
                        }
                }
               if(weekStart.daysBetween(currentDate) ==0 || weekStart.daysBetween(currentDate) == 6){
                       return false;
                } else 
                       return true;
  }


 
dellseysdellseys
Raj V,

Find below an updated version of your code. All I tried to do is to check if today()+1 is a holiday or weekend. does this logic work. The end goal is if i check and today is a holiday or a weekend, i will get the next business day.

With this code i have an error: Method does not exist or incorrect signature: void checkifItisWorkingDay
trigger days on AppChecker__c (before insert, before update) {
 DateTime nextDay, receivedDate;
 List<Holiday> holidays=[Select h.StartTimeInMinutes, h.Name, h.ActivityDate From Holiday h];
  
 for (AppChecker__c newRecord : trigger.new){
     if(newRecord.CreatedDate != null){
         nextDay = newRecord.CreatedDate;
         nextDay = nextDay.addDays(1);
         if(checkifItisWorkingDay(nextDay,holidays)){
                   receivedDate = nextDay;
           } 
     }
 }
   
 public boolean checkifItisWorkingDay(Date currentDate,List<Holiday> holidays){
                Date weekStart = currentDate.toStartofWeek();
                for(Holiday hDay:holidays){
                 if(currentDate.daysBetween(hDay.ActivityDate) == 0){
                    return false;
                     }
                }
                  if(weekStart.daysBetween(currentDate) ==0 || weekStart.daysBetween(currentDate) == 6){
                    return false;
                } else
                    return true;
  }

}

 
dellseysdellseys
Raj V,
Thank you.

I have made some modification to your code. I wanted to know if today is a holiday or weekend. the end goal is if I am able to get this then I used the next business day to store my information. but currently this code is running into error.
 
trigger days on AppChecker__c (before insert, before update) {
 DateTime nextDay, receivedDate;
 List<Holiday> holidays=[Select h.StartTimeInMinutes, h.Name, h.ActivityDate From Holiday h];
  
 for (AppChecker__c newRecord : trigger.new){
     if(newRecord.CreatedDate != null){
         nextDay = newRecord.CreatedDate;
         nextDay = nextDay.addDays(1);
         if(checkifItisWorkingDay(nextDay,holidays)){
                   receivedDate = nextDay;
           } 
     }
 }
   
 public boolean checkifItisWorkingDay(Date currentDate,List<Holiday> holidays){
                Date weekStart = currentDate.toStartofWeek();
                for(Holiday hDay:holidays){
                 if(currentDate.daysBetween(hDay.ActivityDate) == 0){
                    return false;
                     }
                }
                  if(weekStart.daysBetween(currentDate) ==0 || weekStart.daysBetween(currentDate) == 6){
                    return false;
                } else
                    return true;
  }

}
error:
Method does not exist or incorrect signature: void checkifItisWorkingDay()
Raj VakatiRaj Vakati
Use this code 
 
trigger days on AppChecker__c (before insert, before update) {
 DateTime nextDay, receivedDate;
 List<Holiday> holidays=[Select h.StartTimeInMinutes, h.Name, h.ActivityDate From Holiday h];
  
 for (AppChecker__c newRecord : trigger.new){
          nextDay = System.today().addDays(1);
         if(checkifItisWorkingDay(nextDay,holidays)){
                receivedDate = nextDay;
				newRecord.receivedDate__c = receivedDate ;
           
     }
 }
   
   
   
 public static boolean checkifItisWorkingDay(Date currentDate,List<Holiday> holidays){
                Date weekStart = currentDate.toStartofWeek();
                for(Holiday hDay:holidays){
                 if(currentDate.daysBetween(hDay.ActivityDate) == 0){
                    return false;
                     }
                }
                  if(weekStart.daysBetween(currentDate) ==0 || weekStart.daysBetween(currentDate) == 6){
                    return false;
                } else
                    return true;
  }

}

 
dellseysdellseys
@Raj V, Thank you. so what is the currentDate doing here? I now toStartofWeek() which gives or returns the firstday of the week.
dellseysdellseys
It is an argument ignore my question. 
Raj VakatiRaj Vakati
currentDate is always needed to get the today's date.  . Let me know if it's not working ..  
Raj VakatiRaj Vakati
No, Stephen ! It is not the argument and it's nice to have the discussion! 
Raj VakatiRaj Vakati
Is it working?