- 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,872 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 03:24 PM
This seems to work fine with an out-of-box system. My guess is that this is a conflict with the custom application, "Scriptless Scheduled Jobs" - Link to the ServiceNow Share app
- 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-25-2017 06:49 AM
This sounds good. I'm going to give it a shot this morning and monitor the job progress and I'll let you know! Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-25-2017 08:17 AM
Hmm, doesn't seem to be making a difference. I modified the schedule to only run from 7-8 am so I could monitor and see if it actually stopped running after 8.
Here is my when to run tab:
And here is my advanced tab:
I would expect to not see this job waiting to run at 9am in my list of Today's Scheduled Jobs based on the modification of the times in the schedule. It ran at 7, ran again at 8, but should not run at 9:
I'm going to try to keep troubleshooting.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-25-2017 09:36 AM
Couple things:
- The job is still going to be scheduled to run every hour. The only difference is if the script inside the job (the script specified in the Run this script field) gets executed or not. If the job is marked Advanced and and Conditional then the script inside the Condition field will be evaluated. If the script inside the Condition field evaluates to true then the script inside Run this script gets executed - otherwise the job gets scheduled again and, effectively, nothing happens.
- Your screenshot shows a Run this script field that won't actually do anything. So at present I don't believe there is really any way for you to tell if the Condition field is stopping anything or not.
If I were you I'd set the job to run every 5 seconds and put a couple log statements in the Condition field and the Run this script field and then check the Scripts Logs module to see if things are working the way you want.