-2

How can I use Apex to tell if a custom date field falls on a holiday? I've already set up the business hours and holidays in Salesforce.

EDIT: So I currently know how to use the isWithin() method to tell if it falls within business hours, but I need it to go a step further, and tell me if that day is a holiday, not just that it isn't within business hours. Our billing rates are 1.5x for after hours, and 2x for holidays, so I'm trying to find a way to know the difference.

2

1 Answer 1

2

Try this :

// Get the default business hours
BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

// Create Datetime in the local timezone.
Datetime targetTime = Datetime.newInstance(Year from your Date Field, Month from Your Date Field, Day from Your Date Field, 0, 0, 0);

// Find whether the time is within the default business hours
Boolean isWithin= BusinessHours.isWithin(bh.id, targetTime);
1
  • Thank you for the reply! I apologize for not being clear in my original question, but I need to know if that day is a holiday, rather than just that it is not within business hours.
    – ad_rey
    Commented Feb 8, 2022 at 22:44

Not the answer you're looking for? Browse other questions tagged .