- 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:
Solved! Go to Solution.
- 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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2025 07:11 AM
why are you not using GlideSchedule in your calculation?
You can make scheduled job run daily and see if that
see below link and solution from dvp
Schedule job to run last working day of month
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2025 09:03 PM
Hope you are doing good.
Did my reply answer your question?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- 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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2025 01:05 AM
Thanks so much.
