Schedule job triggering every day when it was supposed to trigger on the 5th business day
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2024 04:26 PM - edited 10-03-2024 04:28 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2024 08:49 PM
@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
}
})();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2024 02:30 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2024 12:04 PM - edited 10-05-2024 12:07 PM
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.