Conditional Scheduled job not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-12-2024 09:35 AM
Need to make a scheduled job not run on the first week of a month and not run on FEB and AUG.
Tried this but not working for conditional script of scheduled job
Looking into a conditional script on the months
but also more interested on a conditional query script for the task table to know if a scheduled job has already ran to know the ticket exists and do not run it:
Map of scheduled job
Jan
1 - N (Monthly Task takes this place)
2 - Y
3 - Y
4 - Y
Feb
1 - Y (No Monthly present)
2 - Y
3 - Y
4 - Y
Mar
1 - N (Monthly Task takes this place)
2 - Y
3 - Y
4 - Y
Apr
1 - N (Monthly Task takes this place)
2 - Y
3 - Y
4 - Y
May
1 - Y (No Monthly present)
2 - Y
3 - Y
4 - Y
Jun
1 - N (Monthly Task takes this place)
2 - Y
3 - Y
4 - Y
Jul
1 - N (Monthly Task takes this place)
2 - Y
3 - Y
4 - Y
Aug
1 - Y (No Monthly present)
2 - Y
3 - Y
4 - Y
Sep
1 - N (Monthly Task takes this place)
2 - Y
3 - Y
4 - Y
Oct
1 - N (Monthly Task takes this place)
2 - Y
3 - Y
4 - Y
Nov
1 - Y (No Monthly present)
2 - Y
3 - Y
4 - Y
Dec
1 - N (Monthly Task takes this place)
2 - Y
3 - Y
4 - Y
Conditional script not working
var gr = new GlideRecord('task');
gr.addQuery('assignment_group', 'Home Office Building Operations');
gr.addQuery('short_description', 'Monthly Sprinkler System Maintenance - Home Office');
gr.addActiveQuery();
gr.query();
if (gr.next()) {
// Task found, do not run the scheduled job
gs.log('Task found, skipping scheduled job.');
} else {
// No task found, proceed with the scheduled job
gs.log('No task found, running scheduled job.');
// Add your scheduled job logic here
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-12-2024 11:35 AM - edited ‎08-12-2024 11:36 AM
Determining the month is easy. You can either use getMonthUTC() or getMonthLocalTime() method of the GlideDateTime API for that. First week of a month is going to be trickier because there is no readily available function for that.
As for the conditional script, what exactly is not working? Which block (IF or ELSE) do you land in? Does your GlideRecord query return any results? Have you checked that?
By the way, there is a button on the toolbar for pasting code snippets. It makes your posted code much more readable.
Blog: https://sys.properties | Telegram: https://t.me/sys_properties | LinkedIn: https://www.linkedin.com/in/slava-savitsky/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-13-2024 03:23 AM
@Slava Savitsky
looking to know on how to write the glide record query to be honest the best I came up with for now is:
var gr = new GlideRecord('task');
gr.addQuery('assignment_group', 'Home Office Building Operations');
gr.addQuery('short_description', 'Monthly Sprinkler System Maintenance - Home Office');
gr.addActiveQuery();
gr.query();
if (gr.next()) {
// Task found, do not run the scheduled job
gs.log('Task found, skipping scheduled job.');
} else {
// No task found, proceed with the scheduled job
gs.log('No task found, running scheduled job.');
// Add your scheduled job logic here
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-13-2024 03:27 AM
script needs to be updated to set answer variable as true false
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-13-2024 03:36 AM - edited ‎08-13-2024 03:44 AM
This is the solution I believe may work for weekly tasks, all I would have to do is make sure the run time for those tasks is set to 06:30AM rather than the standard 6AM for all others, and if a monthly is present as an active task in our assignment group, it won't run the weekly task, otherwise, it would run it!
would this work?
var gr = new GlideRecord('task');
gr.addQuery('assignment_group', 'Home Office Building Operations');
gr.addQuery('short_description', 'Monthly Sprinkler System Maintenance - Home Office');
gr.addActiveQuery();
gr.query();
if (gr.next()) {
return true;
}
else {
return false;
}
@Ankur Bawiskar any ideas on the script update to set answer variable as true false