Can I run a scheduled job (on-demand) from a script?

tt-jtw
Kilo Expert

I have a custom script that runs as a Scheduled Job.   There are times that I would like to kick off that job on-demand from another script (or even a workflow).   Is there an API to find the scheduled job and execute it?

1 ACCEPTED SOLUTION
11 REPLIES 11

Yeah I'm betting you're right.   Its probably using all kinds of parameters from the full object.   If you fed just sys_ID that function would have to do a lookup on the table first.


(function executeRule(current, previous /*null when async*/) {

// Create a record in sys_trigger (Scheduled Script Execution)
var trigger = new GlideRecord('sys_trigger');
trigger.initialize();

// Set the name of the scheduled job
trigger.name = 'My Custom Scheduled Job'; // Set the job name

// Set the type of job to "script"
trigger.job_context = 'RunScriptJob'; // Make sure this aligns with the Job ID field

// Set when to run the job (for example, tomorrow at 4 PM)
var nextActionTime = new GlideDateTime();
nextActionTime.setDisplayValue('2024-09-19 16:00:00'); // Set this to the date and time you want
trigger.next_action = nextActionTime;

// Define the script that should be executed
trigger.script = 'gs.info("This is a scheduled script job running at 4:00 PM.");'; // Set your custom script here

// Set the trigger type (Run Once)
trigger.trigger_type = '0'; // 0 = Run Once, 1 = Repeat

// Save the record to schedule the job
trigger.insert();

gs.info('Scheduled script job created.');

})(current, previous);