Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
ShowAll Questionssorted byDate Posted
dellseys

# 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 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++ ){
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;
}```

dellseys
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) {
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;
if(checkifItisWorkingDay(nextDay,holidays)){
}
}
}

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;
}

}```

dellseys
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) {
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;
if(checkifItisWorkingDay(nextDay,holidays)){
}
}
}

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 Vakati
Use this code

```trigger days on AppChecker__c (before insert, before update) {
List<Holiday> holidays=[Select h.StartTimeInMinutes, h.Name, h.ActivityDate From Holiday h];

for (AppChecker__c newRecord : trigger.new){
if(checkifItisWorkingDay(nextDay,holidays)){

}
}

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;
}

}```

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