Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2025 04:00 AM - edited 10-01-2025 04:00 AM
Hi
We tested the following script and it worked fine then:
var currentDate = new GlideDateTime();
// Set the date to the first day of the next month
currentDate.addMonths(1);
currentDate.setDayOfMonth(1);
// Subtract one day to get the last day of the current month
currentDate.addDays(-1);
while (currentDate.getDayOfWeek() === 6 || currentDate.getDayOfWeek() === 7) {
currentDate.addDays(-1);
}
var lastWorkingDayOfMonth = currentDate.getLocalDate();
var lastworkingday = lastWorkingDayOfMonth.getDayOfMonthUTC();
var reportdate = new GlideDate().getDayOfMonthUTC();
var isLastBusinessDay = (lastworkingday == reportdate);
var report_date = new GlideDateTime();
var day_in_week = report_date.getDayOfWeekUTC();
// 1-Monday, 2-Tuesday, 3-Wednesday, 4-Thursday, 5-Friday, 6-Saturday, 7-Sunday
var days_in_month = report_date.getDaysInMonthUTC();
var day_of_month = report_date.getDayOfMonthUTC();
var isLastDay = (day_of_month == days_in_month);
var isWeekend = (day_in_week == 6 || day_in_week == 7);
if ((isLastDay && !isWeekend) || isLastBusinessDay){
// Execute the flow logic here (the code you want to run on the last working day)
gs.log("Running flow on the last working day of the month");
// gs.print("Running flow on the last working day of the month");
runJob();
}
else {
// Optional: Reschedule the flow for the next working day
// Or, skip running the flow and log an error
gs.log("Skipping flow execution: Not the last working day.");
}
function runJob() {
try {
// Execute Synchronously: Run in foreground. Code snippet has access to outputs.
var result = sn_fd.FlowAPI.getRunner().subflow('global.time_sheets_for_current_month').inBackground().run();
//var outputs = result.getOutputs();
// Current subflow has no outputs defined.
} catch (ex) {
var message = ex.getMessage();
gs.error(message);
}
}
It is supposed to trigger a subflow on the last business day of every month. IT did run correctly on the 30th of September. However, it also triggered subflow on the 1st of October.
Can someone please advise why that would have happened ?
Thank you
Solved! Go to Solution.
1 ACCEPTED SOLUTION
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2025 07:12 AM
try this
-> your script was comparing only day numbers without month/year
-> compare full date
var currentDate = new GlideDateTime();
// Set the date to the first day of the next month
currentDate.addMonths(1);
currentDate.setDayOfMonth(1);
// Subtract one day to get the last day of the current month
currentDate.addDays(-1);
// Adjust backwards for weekends until last working day found
while (currentDate.getDayOfWeek() === 6 || currentDate.getDayOfWeek() === 7) {
currentDate.addDays(-1);
}
var lastWorkingDayOfMonth = currentDate.getLocalDate();
// Format dates as yyyy-MM-dd for accurate comparison
var lastWorkingDayDateStr = lastWorkingDayOfMonth.getByFormat('yyyy-MM-dd');
var todayDate = new GlideDate();
var todayDateStr = todayDate.getByFormat('yyyy-MM-dd');
var report_date = new GlideDateTime();
var day_in_week = report_date.getDayOfWeekUTC();
// 1-Monday, 2-Tuesday, ..., 6-Saturday, 7-Sunday
var days_in_month = report_date.getDaysInMonthUTC();
var day_of_month = report_date.getDayOfMonthUTC();
var isLastDay = (day_of_month == days_in_month);
var isWeekend = (day_in_week == 6 || day_in_week == 7);
var isLastBusinessDay = (lastWorkingDayDateStr == todayDateStr);
if ((isLastDay && !isWeekend) || isLastBusinessDay) {
// Execute the flow logic here (the code you want to run on the last working day)
gs.log("Running flow on the last working day of the month");
runJob();
} else {
gs.log("Skipping flow execution: Not the last working day.");
}
function runJob() {
try {
// Execute Synchronously: Run in foreground
var result = sn_fd.FlowAPI.getRunner().subflow('global.time_sheets_for_current_month').inBackground().run();
} catch (ex) {
var message = ex.getMessage();
gs.error(message);
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Regards,
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
5 REPLIES 5
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a week ago - last edited a week ago
var hc = new HolidayChecker();
var result = hc.checkHolidayToday('55ebbbf15f002000b12e3572f2b47714');
var HolidayChecker = Class.create();
HolidayChecker.prototype = {
initialize: function() {
},
checkHolidayToday: function(scheduleSysId) {
var scheduleName = this._checkScheduler(scheduleSysId);
if (!scheduleName) {
return 'NA';
}
var today = this._getTodayInUSCentral();
var match = this._checkSpan(scheduleSysId, today);
return match ? match : 'NA';
},
_checkScheduler: function(schedSysId) {
var scheduleGr = new GlideRecord('cmn_schedule');
if (!scheduleGr.get(schedSysId)) {
return null;
}
return scheduleGr.getValue('name');
},
_checkSpan: function(schedSysId, today) {
var spanGr = new GlideRecord('cmn_schedule_span');
spanGr.addQuery('schedule', schedSysId);
spanGr.addQuery('repeat_type', 'yearly');
spanGr.query();
while (spanGr.next()) {
var yearlyType = spanGr.getValue('yearly_type');
var m, d;
if (yearlyType === 'doy') {
var raw = spanGr.getValue('start_date_time');
if (!raw) continue;
m = parseInt(raw.substring(4, 6), 10);
d = parseInt(raw.substring(6, 8), 10);
} else if (yearlyType === 'float') {
m = parseInt(spanGr.getValue('month'), 10);
var weekday = parseInt(spanGr.getValue('float_day'), 10) - 1;
var occurrence = spanGr.getValue('float_week');
d = this._computeNthWeekday(today.year, m, weekday, occurrence);
} else {
continue;
}
if (m === today.month && d === today.day) {
return spanGr.getValue('name');
}
}
return null;
},
_getTodayInUSCentral: function() {
var nowUtcMs = new GlideDateTime().getNumericValue();
var offsetMinutes = this._getUSCentralOffsetMinutes(nowUtcMs);
var shifted = new Date(nowUtcMs + offsetMinutes * 60000);
var year = shifted.getUTCFullYear();
var month = shifted.getUTCMonth() + 1;
var day = shifted.getUTCDate();
return { year: year, month: month, day: day, str: year + '-' + this._pad(month) + '-' + this._pad(day) };
},
_getUSCentralOffsetMinutes: function(utcMs) {
var utcDate = new Date(utcMs);
var year = utcDate.getUTCFullYear();
var marchSecondSunday = this._nthSundayOfMonth(year, 3, 2);
var dstStartUtc = Date.UTC(year, 2, marchSecondSunday, 8, 0, 0);
var novFirstSunday = this._nthSundayOfMonth(year, 11, 1);
var dstEndUtc = Date.UTC(year, 10, novFirstSunday, 7, 0, 0);
var isDst = utcMs >= dstStartUtc && utcMs < dstEndUtc;
return isDst ? -5 * 60 : -6 * 60;
},
_nthSundayOfMonth: function(year, month, n) {
var daysInMonth = new Date(Date.UTC(year, month, 0)).getUTCDate();
var count = 0;
for (var day = 1; day <= daysInMonth; day++) {
if (new Date(Date.UTC(year, month - 1, day)).getUTCDay() === 0) {
count++;
if (count === n) return day;
}
}
return null;
},
_computeNthWeekday: function(year, month, weekday, occurrenceRaw) {
var daysInMonth = new Date(Date.UTC(year, month, 0)).getUTCDate();
var matches = [];
for (var day = 1; day <= daysInMonth; day++) {
if (new Date(Date.UTC(year, month - 1, day)).getUTCDay() === weekday) {
matches.push(day);
}
}
return occurrenceRaw === 'last' ? matches[matches.length - 1] : matches[parseInt(occurrenceRaw, 10) - 1];
},
_pad: function(n) {
return (n < 10 ? '0' : '') + n;
},
type: 'HolidayChecker'
};