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

Creating Holidays in Test Code

I have been creating a Business Hours Calculation engine in the past few weeks and I have my code working exaclty how I want it and now have moved on to test code. As Bob Buzzard Pointed out, although not well documented Business Hour Data is available in Test Code Context. Holidays, however are not, you have to create your own. This can be verified by this simple test below

private class BusinessHoursMathTest {

    static void SimpleTest(){

        List<BusinessHours> bhs = [select id from BusinessHours where IsDefault=true];
        System.assert(bhs.size() == 1);

        List<Holiday> holidays = [Select Id From Holiday];
        system.assert(holidays.size() == 0);

        List<Account> accs = [select id from Account];
        System.assert(accs.size() == 0);

So I would think that it would be as simple as Querying for Default business Hours, creating a new Holiday, and associating that Holiday with the Business Hours. Something like this

BusinessHours defaultBH = [Select Id From BusinessHours Where IsDefault = True];
Holiday hol = new Holiday();
hol.Name='Test holiday';
hol.activitydate = System.Today();
insert hol;

The holiday is inserted, but there is no affiliation of that Holiday with the Business Hours. I have found that there is no way to affiliate the Holiday with the business hours with Apex. This means I can't properly test my class. I can still get the code coverage I need to deploy, but I can't properly test all logic branches this way. If I wanted to package this that would be a big no no.


Does anyone know of a workaround for this. How can I properly test business hour logic without being able to create holidays and associate them with business hours in the test class. Has anyone come up with a way to do this or can you let me know if I have missed anything or not going about this the proper way. I'm pulling my hair out with this one.
Bradley DelauneBradley Delaune
Hi sfdc_ninja,

To my understanding, Salesforce does not associate Holidays with different sets of Business Hours.  Holiday is a stand-alone object.  I assume that if you insert a Holiday, that holiday would be taken into account in apex.  You didn't provide the class you were attempting to test, so I'm not sure how much I can help you beyond this.

Try inserting the holidays when you are setting up your test invironment before you test your class and see if your class takes that holiday into account.

I'm interested in the result, so let me know how it goes!

Thanks for responding, I appreciate it, although I don't think I quite agree with your assesment that Holidays are stand alone objects as documentation suggests otherwise. There are many references Holidays being associated with particular business hours.  The one I could point ot is on the Holiday object documentation

Represents a period of time during which your customer support team is unavailable. Business hours and escalation rules associated with business hours are suspended during any holidays with which they are affiliated.

Also, just using the UI, whenever you create a holiday, it isnt truely active until you affiliate it with one or more business hours.

There is also an Idea on the idea exchange that states there is an affiliation, it is just not exposed.

When I insert any new holiday in test code, it is not associated with any business hours.  I have tested this many times over.  So inserting a holiday in apex really is useless as there is no way to link it to a particular set of business hours.  

I will add the complete code to my blog when its completed, but this is really driving me crazy trying to write proper tesat code.  As it is, I will likely have to write some test code just to get me the coverage to deploy without being able to fully test all the logic branches and specific scenarios, something I truely hate to have to do.  

I was just hoping someone out there had run into this before and had a workaround.  I know people have written apps on the platform using Business Time calculation, so I'm just curious how they got around this, if they did at all, or they just wrote incomplete test code as I fear I might have to.


Bradley DelauneBradley Delaune
I am horribly mistaken.  I apologize.  You are correct and I hope that this functionality gets added soon.  I'll keep an eye out for someone testing business hours and holidays.

Let us know if you figure it out!