How to add 2 business days excluding weekends and holidays based on scheduled attached

ST9
Tera Contributor

Hi,

I have a requirement  to auto populate a date(in custom field) by adding 2 business days in employee start date. For example-

If the HR profile employee start date is - 3rd April, then a custom field should populate a date of 5th April, excluding weekends/holidays.

I tried creating a before insert BR on HR case table, i am able to add 2 days but cannot exclude weekends/holidays.

(function executeRule(current, previous /*null when async*/) {
  
    // Get the start date of the employee from HR Profile
    var startDate = new GlideDateTime(current.subject_person_hr_profile.employment_start_date);

    // Define the number of days to add
      startDate.addDaysUTC(2);
    
      // Set the value of the date field to the new date
      current.u_task_created_date = startDate;
    
    
  

})(current, previous);

 Please help.

3 REPLIES 3

Anks26
Kilo Sage

Hi ST9, 

You will need to Instantiates a GlideSchedule object and loads the schedule information. 

Locate your business schedule on cmn_schedule table in my example I am using OOB 8-5 weekdays excluding holidays schedule. 

var startDate = new GlideDateTime();
var days = 2;
var dur = new GlideDuration(60 * 60 * 9 * 1000 * days); // replace 9 with number of business hours in your schedule
var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828', 'US/Pacific'); // sys_id of the schedule and time zone, I am using OOB U.S holidays
var end = schedule.add(startDate, dur);
gs.info(startDate);
gs.info(end);


Hope this helps
Thanks
Anks

ST9
Tera Contributor

Hi @Anks26 ,

In our HR Case table we have a custom field called HR schedule which picks up the different schedule based on user's location, so cannot use a specific schedule.

could you please tell what i can do in that case.

 

ST9_1-1680433945276.png

 



You will need to get the schedule sys_id from the current record and then pass it to evaluate the duration. 

var hrSschedule = '090eecae0a0a0b260077e1dfa71da828'; // get the sysid from the current HR record custom field

var scheduleLength = new GlideRecord('cmn_schedule_span');
scheduleLength.get('schedule', hrSschedule); // assuming there is only one schedule entry for each schedule
gs.info(scheduleLength.getDisplayValue('start_date_time'));
var startTime = new GlideDateTime(scheduleLength.getDisplayValue('start_date_time'));
var endTime = new GlideDateTime(scheduleLength.getDisplayValue('end_date_time'));
gs.info(startTime)
gs.info(endTime)

var dur = GlideDateTime.subtract(startTime, endTime); //outputs 9 hours
var diff = dur.getDisplayValue().toString(); // converting output to string
var hrsArray = diff.split(' ')// extracting 9
gs.info(hrsArray[0]);



var startDate = new GlideDateTime();
var days = 2;
var dur = new GlideDuration(60 * 60 * hrsArray[0] * 1000 * days); // dynamic value from the schedule entry
var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828', 'US/Pacific'); // sys_id of the schedule and time zone, I am using OOB U.S holidays
var end = schedule.add(startDate, dur);
gs.info(startDate);
gs.info(end);