- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2016 08:01 PM
Hi Everyone,
We have a requirement from the client that whenever a new record is inserted into a table, the business rule would run to create a schedule job that would only run once at the set time.
Basically every record in this table would contain a time field.
ex: Name Time
Record 1. Amy 12/5/2016 12:00
Record 2. Jean 22/5/2016 08:00
Record 3. James 20/5/2016 07:00
Whenever someone create a new record, a new schedule job should be created, set to run script, run only once and start at the specific time we enter for that record.
So if we create the three records above, three schedule jobs should be created and run once at the time specified in the time field above.
I am very new to scripting in ServiceNow so just wondering if there is any script that is for create new schedule job and set parameters as well?
Thank you!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2016 08:28 PM
Hi Kiki,
Yes, this can be done. What sort of scheduled job are you looking for? A report? A script? You can run an AFTER business rule to insert a new record in the sysauto_script table (for a script.) If you want to run some other job, let me know.
Something like this (untested)
Name: Create new scheduled job
When: After
Table: (your table full of records)
Insert: true
Update: false
Advanced: true
Condition: (empty)
Script:
function onAfter(current, previous) {
var s = new GlideRecord('sysauto_script');
s.newRecord();
s.name = current.name;
s.active = true;
s.run_type = 'once';
s.run_start = current.time;
s.script = '// Insert your Javascript here';
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2020 05:24 AM
Scheduled jobs don't have a 'current' object. You're sending a null object to the notification.
If you want to pass a record (or series of records and series of events), you'll have to do a GlideRecord lookup. In which case, I would move the Owner Group is not empty check to the GlideRecord lookup in the scheduled job. Something like this:
var cGr = new GlideRecord('sc_cat_item');
cGr.addNullQuery('u_owner_group');
cGr.query();
while (cGr.next()) {
gs.eventQueue('catalogitem.noti', cGr, '', '');
}
Note, code is untested and may require modifications to work on your system.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2020 07:11 AM
FYI - A response to this post was featured on the Community Live Stream video. I invite you to watch the episode.
Video: Community Live Stream - 2020-01-23 - Developer Community - ServiceNow Community
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2020 11:48 PM
Thanks very much Chuck.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2022 06:38 AM
Hi Chuck;
Thanks for this above script which will help to create scheduled jobs through BR. My requirement to schedule a job for a later time needs to trigger a notification per the follow-on time on the incident record. I wrote below in BR. But "gs.eventQueue" not passing the actual value of "current.assigned_to" to the scheduled job. Due to this it failed to send notification due to not found assigned to. Can you please help on this,
-----------------BR------------
var job = new GlideRecord('sysauto_script');
job.newRecord();
job.name = current.number;
job.active = true;
job.run_type = 'once';
job.run_start = current.follow_up;
job.script = 'gs.eventQueue(follow_up_time,current,current.assigned_to);';
job.insert();
-----------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2016 09:28 PM
Hi Kiki,
Out of the box there's a script include BillingReportUtil which has a function to create a scheduled job on the fly. It's similar to what Chuck shared but I'm adding it just for reference in case it helps and also because it's a production "tested" version i guess :
the function is called from a business rule on the following way:
var billingUtil = new BillingReportUtil();
billingUtil.createScheduledJob(current, 'sysauto_script', 'Amazon', 'AwsBillingReportUtil');
The method createScheduledJob within the BillingReportUtil script include is the following:
// Used for report schedule business rule to generate/update scheduler
createScheduledJob : function(currentGr, schedulerTable, provider, scriptName) {
var scheduleJobId = currentGr.scheduler;
var gr = new GlideRecord(schedulerTable);
var name = provider + ' Billing Report Scheduler Job for : ' + currentGr.name;
if (currentGr.scheduler.nil()){
gr.name = name;
gr.active = true;
var script = 'var billingUtil = new BillingReportUtil(); \n';
script = script + 'if(!billingUtil.isBillingWorkflowExecuting(\''+currentGr.sys_id+'\'));\n';
script = script + ' billingUtil.runThisJob(\''+currentGr.sys_id+'\');';
script = script.replace('BillingReportUtil', scriptName);
gr.setValue('script',script);
var sysId = gr.insert();
currentGr.setValue('scheduler',sysId);
if(provider == 'Azure')
currentGr.update();
}
else{
gr.get(currentGr.scheduler);
gr.name = name;
gr.update();
}
}
Thanks,
Berny