The CreatorCon Call for Content is officially open! Get started here.

Schedule job triggering every day when it was supposed to trigger on the 5th business day

1__TimothyO
Tera Contributor

I wrote a script for a schedule job that triggers every 5th business day of the month of May and November without the cmn_schedule table and this keep running everyday. Below is the code i wrote, please i would appreciate any assistance here.

(function executeScheduledJob() {
    var today = new GlideDateTime();
    var currentMonth = today.getMonthLocalTime();

    // Check if it's May or November
    if (currentMonth === 5 || currentMonth === 11) {
        var firstDayOfMonth = new GlideDateTime(gs.beginningOfThisMonth()); // Get the 1st day of the month
        var businessDayCount = 0;

        // Loop through the days from the 1st to today
        while (firstDayOfMonth.getLocalDate().compareTo(today.getLocalDate()) <= 0) {
            var dayOfWeek = firstDayOfMonth.getDayOfWeekLocalTime(); // 1 = Monday, 7 = Sunday

            // Check if it's a weekday (Monday to Friday)
            if (dayOfWeek >= 1 && dayOfWeek <= 5) {
                businessDayCount++; // Increment the business day count
            }

            // Check if today is the 5th business day
            if (businessDayCount === 3 && firstDayOfMonth.getLocalDate().compareTo(today.getLocalDate()) === 0) {

                gs.log("Running scheduled job on the 5th business day of May/November!");
                return true;
                // Job runs on the 5th business day
            } else {


                // If not in May or November, or not the 5th business day, return false
                gs.info("Today is not the 5th business day of May/November. Scheduled job will not run.");

                // Move to the next day
                firstDayOfMonth.addDaysLocalTime(1);
            }


        }
    }

})();
3 REPLIES 3

Siddhesh Jadhav
Kilo Sage
Kilo Sage

@1__TimothyO 

please try following script

(function executeScheduledJob() {
    var today = new GlideDateTime();
    var currentMonth = today.getMonthLocalTime();
    var businessDayCount = 0;

    if (currentMonth == 5 || currentMonth == 11) {
        var firstDayOfMonth = new GlideDateTime(gs.beginningOfThisMonth()); //  1st day of the month

        // Loop through the days from the 1st to today
        while (firstDayOfMonth.getLocalDate().compareTo(today.getLocalDate()) < 0) {
            var dayOfWeek = firstDayOfMonth.getDayOfWeekLocalTime(); // 1 = Monday, 7 = Sunday

            // Check if it's a weekday (Monday to Friday)
            if (dayOfWeek >= 1 && dayOfWeek <= 5) {
                businessDayCount++; // Increment business day 
            }

            // Move to the next day
            firstDayOfMonth.addDaysLocalTime(1);
        }

        // Check if today is the 5th business day
        if (businessDayCount == 5) {
            gs.log("Running scheduled job on the 5th business day of May/November!");
            // Job runs on the 5th business day
            return true;
        } else {
            gs.info("Today is not the 5th business day of May/November. Scheduled job will not run.");
            return false; // Ensure the job does not run if it's not the 5th business day
        }
    } else {
        gs.info("Not May or November. Scheduled job will not run.");
        return false; // Ensure the job does not run if it's not May or November
    }
})();

 

I tried using this script but it still gets trigger the case everyday(days that are not the 5th business day) instead of the waiting on the 5th business day. I still have the same problem, what could till be the problem?

 

 

var gdt = new GlideDateTime();
for (i = 0; i < 8; i++) {
	gs.info('DateTime = ' + gdt + ' day of month = ' + gdt.getDayOfMonthLocalTime());
	if (gdt.getDayOfMonthLocalTime() == 5)
		gs.info("The 5th");
	gdt.addDays(1);
}

 

results in:

 

*** Script: DateTime = 2024-10-05 19:05:43 day of month = 5
*** Script: The 5th
*** Script: DateTime = 2024-10-06 19:05:43 day of month = 6
*** Script: DateTime = 2024-10-07 19:05:43 day of month = 7
*** Script: DateTime = 2024-10-08 19:05:43 day of month = 8
*** Script: DateTime = 2024-10-09 19:05:43 day of month = 9
*** Script: DateTime = 2024-10-10 19:05:43 day of month = 10
*** Script: DateTime = 2024-10-11 19:05:43 day of month = 11
*** Script: DateTime = 2024-10-12 19:05:43 day of month = 12

 

in Scripts - Background.