1

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

You should change the method as below.

If schedDateTime is within business hour then no need to do alteration, otherwise, it will return the nearest next datetime.

Here nextStartDate will take two parameters BusinessHourId and input Datetime, as opposed to only BusinessHourId which you have used.

public Datetime scheduler(String varFrequency, Datetime schedDateTime)
{

    BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault = true];

    if (!BusinessHours.isWithin(bh.Id, schedDateTime))
    { 
        return BusinessHours.nextStartDate(bh.Id,schedDateTime);
    }
    return schedDateTime;
}

public Date returnSchedulerDate(String varFrequency, Datetime schedDateTime)
{
    DateTime dt = scheduler(varFrequency,schedDateTime);
    return Date.newinstance(dT.year(), dT.month(), dT.day());
}
  • thank you, that was a typo on my part. You are right, I missed the second parameter. My issue is that I don't want to take the time into consideration. The situation where schedDateTime is on the right day but not on the right time is going to happen often. Is there a way around this? – Olivia Aug 8 '18 at 18:27
  • this should the right time too. Just for sake of testing, make Business hour's timezone and user's timezone same. You will get the correct time – Santanu Boral Aug 8 '18 at 18:39
  • Santanu, even if I change the timezones to be the same, I do not want to take into account the hours. If a user submits a time outside of the core business hours, I still want to use that DATE. Does that make sense? – Olivia Aug 9 '18 at 12:48
  • so what it is returning when you submit outside of business hours? – Santanu Boral Aug 9 '18 at 13:00
  • If you want the date from datetime which the method is returning then create a separate method the way I have updated my answer – Santanu Boral Aug 9 '18 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.