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

jcote
Giga Expert

Hi Joanne,



I am not aware of a way to specifically call a scheduled job to run, outside of the scheduled job. Maybe someone else can chime in on that as I'd be interested in hearing if there is.



But if that requirement came my way, I would just take the script in the scheduled job, place it in a Script Include, and then call the necessary functions from the Script Include from a Business Rule or UI Action, etc.


Uncle Rob
Kilo Patron

Never tried this myself, but....



- Take a look at a scheduled job of type "Script Execution".   Notice there's an Execute Now button?


- Take a look through UI Actions.   There's an Execute Now button on the Scheduled Jobs table.   Sweet!


- Open the Execute Now UI Action (the one for Scheduled Jobs).


- Check out the script.



SncTriggerSynchronizer.executeNow(current);



Replace "current" with the sys_id of whatever scheduled job you're trying to execute on-demand, and you should be good to go.



I'd test this first with a Scheduled Job that just dumps a gs.log.  


Hello,

 

Just in case: Here is an example for this (the reports need to be run by each recipient). In general it works, but imo does not really cover the issue with elegance (tl:dr: I do not like it).

 

var recipients = getLocationOwners();
gs.log(recipients.toString());
if(recipients.length > 0)
	runScheduledReports(recipients);

function runScheduledReports(recipients)
{
	var scheduledReport = new GlideRecord('sysauto_report');
	scheduledReport.addQuery('name','Scheduled Execution of Compliance - SACM my locations');
	scheduledReport.query();
	if(!scheduledReport.next())
		return;
	
	for(var i=0; i<recipients.length; i++)
		{
		var recipient = recipients[i];
		runScheduledReport(scheduledReport, recipient);
	}
}

function runScheduledReport(scheduledReport, recipient)
{
	scheduledReport.user_list = recipient;
	scheduledReport.run_as = recipient;
	scheduledReport.update();
	
	SncTriggerSynchronizer.executeNow(scheduledReport);
}

 

Greetings

Fabian

edwin_munoz
Mega Guru

Hello Joanne,



Please refer to this section in the documentation:



http://wiki.servicenow.com/index.php?title=Creating_a_Scheduled_Job#Executing_Scheduled_Jobs_from_Sc...



Thank you