How to programmatically create a holiday set

lliu (2 posts)
July 29, 2020 09:59 AM
Accepted Answer

We manually create a holiday set in the adtempus console. Instead of manually creating it, is there a way to programmatically create one? I see the API has a GetHolidaySet() method but theres none to create one. Essentially I want to add a list of dates that will be used as my holiday set.

Thanks!

Bill Staff (599 posts)
July 29, 2020 10:36 AM
Accepted Answer

Holiday Sets use the SharedSchedule class, and there's a generic DataContext.CreateObject method that is used for all object creation. Here's a sample that creates holidays for specific dates. If you want these dates to match only in specific years, you need to create a new DateCriterion for each year, with the StartDate and EndDate set as shown below.

It's possible (and much simpler programmatically) to set the year directly on the DaySpecification, but the UI doesn't handle that case and you won't be able to tell in the Console that the rule only applies to a specific year  (we have a bug report in to fix that).

 

    using (var session = Scheduler.Connect(".", LoginAuthenticationType.Windows, "", ""))
    {
        using (var context = session.NewDataContext())
        {
            //Create a Holiday Set, which uses the SharedSchedule class
            var holidaySet=context.CreateObject(ClassID.SharedSchedule) as SharedSchedule;
            
            //configure it as a holoday set
            holidaySet.UseAsHolidaySet=true;
            
            //be sure the name is unique.
            holidaySet.Name="My Custom Holidays";

            var criterion = context.CreateObject(ClassID.DateCriterion) as DateCriterion;
            //if you want the rule to apply only for a particular year (or other time range), set the 
            //start and end dates here. Otherwise, the holiday will repeat on the specified day every year.
            //criterion.StartDate=new DateTime(2020,1,1);
            //criterion.EndDate=new DateTime(2020,12,31);

            //Set the criterion to use specified days (instead of being an interval-based criterion)
            criterion.CriterionType= DateCriterionType.SpecifiedDays;
            
            //for each day, create a rule
            criterion.Days.Add(CreateRuleForDate(context, 8, 15));
            criterion.Days.Add(CreateRuleForDate(context, 9, 12));

            holidaySet.DateCriteria.Add(criterion);
            
            //if you want additional rule sets that apply to different date ranges,
            //create a new DateCriterion for each year/date range, and add the appropriate dates to it as above.
            
            holidaySet.Save();



        }

    }
    
    //Create a DaySpecification that applies only to a specific month and day.
    DaySpecification CreateRuleForDate(ArcanaDevelopment.adTempus.Client.DataContext context, int month, int day, string description=null)
    {
        //create a DaySpecification, which is a rule to match dates. 
        var spec=context.CreateObject(ClassID.DaySpecification) as DaySpecification;
        
        //DaySpecification can be used for rules like "every Tuesday" but we just want it to match a specific month and day
        spec.SetMonth(month,true);
        spec.SetDay(day,true);
        //don't set spec.Year even if you want this rule to apply only to 1 year. It will work but the UI doesn't show the year
        //so it can't be edited there. Set the date range for the DateCriterion instead.
        
        //optionally set a name to describe this date in the UI, like "Annual company picnic". If you don't have a name, the UI just shows the date
        spec.Name=description;
        
        return spec;
    }

lliu (2 posts)
August 4, 2020 05:18 PM
Accepted Answer
Thanks Bill, I will try it out!

Replies are disabled for this topic.