I am trying to create a payment schedule off of a input field DATE
. If the date falls on a Weekend or on a Holiday then I want to move the payment date to the next open business day.
I was using the BusinessHours
class to help me solve this but the business hours for the company takes into consideration the business hours. ha I know. I do not want to include the business hours, just the days. I also don't want to have to change the core business hours just to make this logic work.
For example: a user schedules a payment date of Monday at 5am. The business hours don't start until 7am. The logic will keep searching for the next available business day.
Is there a better class that only takes into consideration the business DAYS or should I work around the hours by pulling the DateTime, stripping the time (even though when I do this it still leaves a 00:00:00 time), and then pushing to the nextStartDate()
?
Thoughts?
Here is some code I started:
public Datetime scheduler(String varFrequency, Datetime schedDateTime){
BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault = true];
if (BusinessHours.isWithin(bh.Id, schedDateTime)){ //this will come up as FALSE if the time isn't between 7am - 6pm M-F
schedDateTime = schedDateTime.addDays(1);
}
else {
schedDateTime = BusinessHours.nextStartDate(bh.Id, schedDateTime);
}
}