schedule job to run on every 11th business day excluding holidays
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2023 01:42 PM
My requirement was to run a particular script on 11th business day each month excluding weekends and UK holidays.
I have worked around and fused my brain on this and came up with a solution.
Hello community, please check this solution and correct if any improvements.
Solution: We will count each day from the first day of month (first_day) and check if it is in schedule and keep a count of number of holidays and regular weekdays while incrementing first_day. Then we will check the regular day counter is equals to 10 and today's date equals to the current date of first_day, this will give us the 11th business day in the current month.
Steps:
1. create a new script execution from sysauto_scripts>New , keep Run as Daily and give your preferred time
2. Check for the Conditional checkbox and it appears an advanced condition section where you can write your condition logic.
3. in the condition section write your logic. for me it is this code to satisfy above scenario:
function checkIfEleventhDay() {
var grSchedule = new GlideRecord('cmn_schedule'); //GlideRecord obj for schedule table
if (grSchedule.get('name', 'weekdays and uk holidays')) //returns true if record exists
{
var sched = new GlideSchedule(grSchedule.sys_id); //Instantiates a GlideSchedule object and loads the schedule information
var today = new GlideDateTime(); //Present Date
var first_day = new GlideDateTime(gs.beginningOfThisMonth()); //First date of this month
//gs.print("first day: "+first_day.getLocalDate());
var day_count = 0; //regular day counter
var holiday_count = 0; //holiday counter
var flag = false; //flag bool
//gs.print("present day: "+today.getLocalDate());
for (var i = 1; i <= today.getDayOfMonthLocalTime(); i++) //run loop till today's date to count number of holidays and regular weekdays
{
if (!sched.isInSchedule(first_day.getLocalDate())) //checks each day if it is in weekday and uk holiday schedule
{
//gs.print("not in schedule: holiday on "+ first_day.getLocalDate());
holiday_count = holiday_count + 1; //increment holiday if today is holiday
first_day.addDaysLocalTime(1); //increment to next day
} else {
//gs.print("in schedule: normal day on "+ first_day.getLocalDate());
day_count = day_count + 1; //increment week/working day counter
first_day.addDaysLocalTime(1); //increment to next day
//gs.print(today.getLocalDate().toString() +" , "+ first_day.getLocalDate().toString());
if ((day_count == 10) && (today.getLocalDate().toString() == first_day.getLocalDate().toString())) //if day count is 10 and today's date and 11th business date matches
{
//gs.print("!!!! IT IS 11TH DAY !!!! on "+first_day.getLocalDate());
flag = true; // make flag true
break; // exit the loop
}
}
}
//gs.print(flag);
//gs.print("holiday : "+holiday_count);
//gs.print("regular: "+day_count);
if (flag == true)
return true; //functions return true and the "Run this script" script executes
else
return false; //nothing will happen for the day
}
return false; //nothing will happen for the day
}
checkIfEleventhDay(); //function call
4. then in Run this Script you can write the script you want to execute after the condition is true
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2023 05:20 AM
// Get the current date
var now = new GlideDate();
// Get the next 11th business day
var nextDate = new GlideDateTime();
nextDate.addDays(11);
while (gs.isHoliday(nextDate) || gs.isWeekend(nextDate)) {
nextDate.addDays(1);
}
// Schedule the job to run on the next 11th business day
var job = new GlideRecord('sysauto_script');
job.initialize();
job.name = "My Job";
job.script = "email_script";
job.run_type = "once";
job.next_action = nextDate;
job.insert();
try this code