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
china.leafchina.leaf 

How can i judge a datetime is holiday?

I want to know whether a datetime is holiday through the setting of holiday?

 

thank you!

DevAngelDevAngel

Hi china.leaf,

 

Due to various international holiday schedules, arbitrary movement of holidays and so on, you need to create a custom object to hold your holidays and keep that up to date based on your corporate holiday schedule.

 

 

Cheers

china.leafchina.leaf

Can I use "Administrator Setup -> Company Profile -> Holidays" ?

GuyClairboisGuyClairbois

what about this, making use of the standard Holiday and WorkingDays specified in Salesforce.com:

 

    global static Boolean isHoliday(Date checkDate){
        BusinessHours stdBusinessHours = [select id from Businesshours where isDefault = true];
        Date outDate = (BusinessHours.add(stdBusinessHours.id, checkDate, 1)).date();
        if(checkDate == outDate){
            return false;
        } else {
            return true;
        }       
    } 

 

craigmhcraigmh

Yeah, you'll have to code a method querying the Holiday sObject, and also set up the data in the sObject. Not only do you have to worry about timing, but also recurring Holidays, localization, things like that.

 

The good news is that if you write something that works well, you could probably sell it for a good profit on AppExchange.

GuyClairboisGuyClairbois

My solution actually does not require the custom object. It uses the standard salesforce calendar and holidays. So recurring holidays and also localization are taken into consideration (you'd have to make the query parameter-based if you have different holiday calendars in your org).

 

The solution queries salesforce for the working day 1 millisecond after the input date. It's a special WorkingDays functionality that was pointed out to me by ForceMonkey here: http://forcemonkey.blogspot.com/2009/11/calculating-business-hours.html

 

If the date returned is the same as the inputted date, then the inputted date is a working day. If the values are different, then it is not a working day. 

 

So this is really all you have to do..

craigmhcraigmh

Wow, that's pretty handy.

 

Setup => Administration Setup => Company Profile => Fiscal Year/Business Hours/Holidays

 

Nice...now if only you could make holidays recur on days like the fourth Thursday or the Tuesday after the first Monday of a month.

GuyClairboisGuyClairbois

Well 'fourth Thursday' is already possible (see e.g. the Monthly and Yearly recurring holidays). 

 

Some holidays will always be difficult to program though, e.g. Easter..  .So no other option than manual entry there..

craigmhcraigmh

Weird, I didn't see the full recurring holiday controls until I edited the holiday. Annoying that they're not there when creating the holiday.

VAHBVAHB

Very smart, that was a great idea

Jeffrey KimballJeffrey Kimball

Sorry for "necromancing" this thread.. but I had to look up the "stat holiday" answers I could find on developer.sfdc after reading this gem of a lie that I wish was true on a Trailhead training article.. LOL

User-added image

The innate org details mentioned above don't do anything for orgs with branches accross multiple countries and states.. Love it.. LOVE IT! :-)