- 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 10:03 AM
I have seen people successfully use the Calendar field on the scheduled job form (sys_trigger table). By default every system has one calendar called "Mon thru Friday 9-5". You could modify the out-of-box one or create your own calendar if you like to be used by your scheduled job. The only thing that relies on the default calendar is the old (deprecated) SLA engine - unless you are running ServiceNow Express version that still uses the old SLA engine.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2017 10:11 AM
I had actually just noticed that field recently, but it appears to be blocked out for existing jobs. Can I somehow add the calendar to my existing job or do I need to re-create it?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2017 10:41 AM
There is an ACL that blocks you from changing any fields on the sys_trigger record of a scheduled job that is based on a scheduled job definition from any of the tables that extend from the sysauto. Sorry if that is a confusing explanation. Basically it means you can't change a job from the sys_trigger table is the job is defined in a document in the sysauto tables (sysauto_report, sysauto_script, etc.)
/nav_to.do?uri=sys_security_acl.do?sys_id=cbfe0d80c310010091fefd251eba8ff3
function canWriteSysTrigger() {
var docId = current.document_key + "";
if (gs.nil(docId)) {
return true;
}
var baseTable = GlideDBObjectManager.getAbsoluteBase(current.document + "");
if (baseTable != "sysauto") {
return true;
}
return false;
}
canWriteSysTrigger();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2017 10:30 AM
I tried testing out your script with some sample date times and it appears that GlideSchedule.isInSchedule() works fine for me. I wonder what is going wrong in your case. Keep in mind that your script is returning "false" if the things are "in schedule" and if not then it returns "true". Maybe that logic is backward?
I ran the following in Scripts - Background:
function checkInSchedule(time){
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(time);
if(inSched){
return false;
}
else{
return true;
}
}
var times = {
rightnow : new GlideDateTime(),
tomorrow : new GlideDateTime(),
yesterday : new GlideDateTime(),
twoDaysAgo : new GlideDateTime()
}
times.tomorrow.addDays(1);
times.yesterday.addDays(-1);
times.twoDaysAgo.addDays(-2);
var days = ["", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
for (time in times) {
gs.print(time + ", " + days[times[time].getDayOfWeek()] + " " + times[time] + " is in schedule: " + checkInSchedule(times[time]));
}
Here is the output:
[0:00:00.196] Script completed in scope global: script
*** Script: rightnow, Mon 2017-04-24 17:24:24 is in schedule: false
*** Script: tomorrow, Tue 2017-04-25 17:24:24 is in schedule: false
*** Script: yesterday, Sun 2017-04-23 17:24:24 is in schedule: true
*** Script: twoDaysAgo, Sat 2017-04-22 17:24:24 is in schedule: true