- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2017 08:09 AM
I'm trying to create a scheduled job that will run only at certain times. In this case, I want it only to run weekdays, during business hours, and not on holidays. All this job does is fire off an event that I can use to trigger a notification.
I've found some useful code, but it doesn't seem to be sticking to the schedule times, as I'm seeing the job kick off on weekends, etc.
Am I missing something?
checkInSchedule();
function checkInSchedule(){
var schedRec = new GlideRecord('cmn_schedule');
schedRec.get('8-5 weekdays excluding holidays');
if(typeof GlideSchedule != 'undefined')
var sched = new GlideSchedule(schedRec.sys_id);
var inSched = sched.isInSchedule(new GlideDateTime());
if(inSched){
return false;
}
else{
return true;
}
}
As always, any help is appreciated!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- 10,888 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2017 04:06 PM
Yeah... that's the problem. Normally, out-of-the-box, you can only run a scripted condition if the "advanced" checkbox has been checked on the Scheduled Job. However, when you install the "Scriptless Scheduled Jobs" application it exposes the Condition field even when the Advanced field has not been checked. This leads to a confusing situation where you can put something in the scripted condition field and it will be ignored. You actually can get what you want though, Dan. Do this:
- Leave your job exactly as it is right now (both the Conditions and Condition field filled out and all the other stuff for the Scriptless behavior)
- Click the "Advanced" checkbox (your Scriptless behavior fields will disappear but they will still work!)
- Save your changes
Now what will happen is that the platform will evaluate the script in the Condition field and if it evaluates to true then it will fire the ScriptlessScheduledJobUtils script include and the Conditions (u_conditions) field will be evaluated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2017 10:41 AM
That could be the case, but what I'm seeing is that it's just running no matter what. The event is showing up every hour, on the hour regardless of my script.
I've tried creating a new schedule item in the sys_trigger table using the calendar field, and changed the schedule for today to end in about 20 minutes so I am hoping to see it execute then, but not after. Will update with results.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2017 10:43 AM
Can you show me the code that is handling the return value of checkInSchedule? How are you using it?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2017 10:50 AM
I am admittedly a low-coder, but ideally this scheduled job looks for call tickets that haven't been worked in over an hour. It will create an event that triggers a notification to the support staff to review the ticket soon.
I grabbed this code from another discussion revolving around a similar topic, and this seemed to work for them to return results based on schedules. I am less concerned about the true or false aspect of the code because I can flip-flop that once I'm confident that the schedule itself is being used to determine whether or not to run the job... right now it's just running on the repeat interval regardless.
I did notice an out of scope indicator on line 7, but wasn't sure that mattered given the number of times I've seen this code provided as a solution for this problem.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2017 11:03 AM
What Class of scheduled job is that? What version of serviceNow are you running?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2017 11:13 AM
Running Helsinki, patch 7. Not sure where to find the class, but when I select to create a new scheduled job, I'm using the "Automatically run a script of your choosing" option.
Side note, it seems like the calendar option worked when i created a new record in the sys_trigger table with the same data as the other record that I couldn't edit the calendar field on. I saw that it did not have another waiting entry in the today's scheduled jobs list while the original one did. While this worked, I'd still like to perfect the script that only runs weekdays 8-5 because i can think of many use-cases for this.