Scheduled Job always running despite conditional check?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2023 09:18 AM
Hi,
I have a report that I only want to run on the second Tuesday of each month. I have created a conditional script to check, and it returns the correct results when I run it in a background script. However when I setup the conditional script in the scheduled job it always runs even when my checkCondition() function only has return false options. Any ideas what is happening here?
//script should run only on 2nd tuesday of the month
checkCondition();
function checkCondition(){
var gdt = new GlideDateTime();
//if day of the month is between 8-14 and its the 2nd day of the week (Tuesday), run this script
if((gdt.getDayOfMonthLocalTime() >= 8 && gdt.getDayOfMonthLocalTime() <= 14) && (gdt.getDayOfWeekLocalTime() == 2)) {
//if((gdt.getDayOfMonthUTC() >= 8 && gdt.getDayOfMonthUTC() <= 14) && gdt.getDayOfWeekUTC() == 2){ //check if LocalTime vs UTC effects issue
//return true;
return false;
}
else{
return false;
}
}
(I removed the email addresses for the screen shot)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Yeah this essentially what I ended up doing.
answer = false;
var gdt = new GlideDateTime();
//if day of the month is between 8-14 and its the 2nd day of the week (Tuesday), run this script
if((gdt.getDayOfMonthLocalTime() >= 8 && gdt.getDayOfMonthLocalTime() <= 14) && (gdt.getDayOfWeekLocalTime() == 2)) {
answer = true;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I faced the same issue. The problem is that using "var" makes the variable locally scoped to that specific block. To fix this, you should define the variable without "var" to ensure it is accessible, or simply ensure the result is the final expression evaluated.
In your case, use an expression like "answer = true;" or "answer = false;. Place this at the very end of your code block and do not use the "return" keyword. ServiceNow internally interprets the last line of the script as the final output.
