Script not working in scheduled reports
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2025 10:27 PM - edited 06-04-2025 10:30 PM
Hi Team,
Good Day !
I wrote a script in Scheduled report and it's not working and when I run same script in background script it's working fine.
The scenario is a scheduled report that is supposed to run on the second Monday of each quarter . However, it is incorrectly triggering on the first Monday of every week.
I am attaching the script and background script executions as well.
Please check and do the needful !
Thanks & Regards,
Sai Sireesha G
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2025 04:09 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2025 04:57 AM
I believe logs don't come from condition script field in scheduled report
But this logic you can use to check if it's 2nd monday of each quarter using native Date() object
(function() {
var answer = false;
var now = new Date(); // Current date/time
var month = now.getMonth(); // 0=Jan, 1=Feb, ..., 11=Dec
var dayOfWeek = now.getDay(); // 0=Sunday, 1=Monday, ..., 6=Saturday
var date = now.getDate(); // 1-31
// Check if this is the first month of a quarter
if (month === 0 || month === 3 || month === 6 || month === 9) {
// Check if today is Monday
if (dayOfWeek === 1) {
// Check if this is the 2nd Monday (date between 8 and 14)
if (date > 7 && date <= 14) {
answer = true;
}
}
}
// For ServiceNow Scheduled Reports, the script must return true/false
return answer;
})();
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
06-06-2025 12:52 AM
Hi @Ankur Bawiskar ,
As you suggested, I have updated the same, As per the condition we shouldn't receive report but still we are receiving it.
I have attached the screenshot as well.
Please have a look and let me know your feedback
Regards,
Sireesha G
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2025 02:06 AM
it worked fine for me
14th July 2025 is 2nd Monday of 3rd quarter, so it returned true for me
Remember when you use Date() object the months start from 0, 1, 2
0 sunday, 1 monday .....
(function() {
var answer = false;
var now = new Date(2025, 6, 14); // Example: July 14, 2025 (second Monday of Q3)
var month = now.getMonth(); // 0=Jan, 1=Feb, ..., 11=Dec
var dayOfWeek = now.getDay(); // 0=Sunday, 1=Monday, ..., 6=Saturday
var date = now.getDate(); // 1-31
// Check if this is the first month of a quarter (Jan, Apr, Jul, Oct)
if (month === 0 || month === 3 || month === 6 || month === 9) {
// Check if today is Monday and the 2nd Monday (date between 8 and 14)
if (dayOfWeek === 1 && date >= 8 && date <= 14) {
answer = true;
}
}
gs.info('Is second Monday of a quarter? ' + answer);
})();
Try passing different values and see and then add that code in scheduled report, it should work
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
06-05-2025 12:24 AM
Hi @Sireesha7 ,
is it a scoped add the scheduled job is in?
gdt.getMonthUTC()
intead of geMonth
gd.getDayOfMonthUTC()
intead of getDayOfMonth
var gdt = new GlideDate();
var day = gdt.getDayOfMonthNoTZ()
var month = gdt.getMonthNoTZ()
answer = (day > 7 && day < 14) && (month == 1 || month == 4 || month == 7 || month == 10);
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya