Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Triggering scheduled job from URL

cborkert
Giga Contributor

Is there a URL that can trigger a scheduled job (on demand) to run, so I can trigger a script to run from an external system that will send a HTTP get to ServiceNow, with the name of the script (scheduled job)?

1 ACCEPTED SOLUTION

cborkert
Giga Contributor

All great answers thanks. In case anyone is interested I went with a processor.. Processor name and parameters set to "EX".


Code follows..



// remotely execute a scheduled job, restricted to admin only


(function(){


  if(!gs.hasRole('admin')) return;


  var name = g_request.getParameter("name");


  var scriptname = new GlideRecord('sysauto_script');


  scriptname.get('name', name);


  if(!scriptname) return;        


  SncTriggerSynchronizer.executeNow(scriptname);


  g_processor.writeOutput('','ok ');


})();



call like so https://instance.service-now.com/?EX&name=<script>


View solution in original post

4 REPLIES 4

amadosierra
Kilo Guru

Hi Chris,



How would you deal with authentication from the external system?


Here's an idea on how to do it (once the user is authenticated):


  1. Create a new table (e.g. u_run_job) that will log a record every time the external system calls the URL.
  2. Include a reference field for the scheduled job to run (e.g. u_scheduled_job).
  3. Create a Client Script onLoad that reads the value for the schedule job reference id and saves the record.
  4. From the external system (or from a browser for testing purposes), call the URL as follows:
    https://<instance name>.service-now.com/nav_to.do?uri=u_run_job.do?sys_id=-1%26sysparm_query=u_scheduled_job=<job_id>


It is not required to create a new record every time but I think it could be a good idea to have a log.


Brad Tilton
ServiceNow Employee
ServiceNow Employee

I don't think this functionality exists by default, but you could pretty easily write a ui page that accepts a url parameter and triggers a scheduled job.


ohhgr
Kilo Sage

Hi Chris,



You could also do it using Direct Web Services.



1 ) Create a new boolean field on scheduled jobs, say "Run now".


2 ) Write a BR that will run the job on change of Run now field.


3 ) Using SOAP / REST you can update the scheduled job record's run now field.



Thanks,
Mandar


cborkert
Giga Contributor

All great answers thanks. In case anyone is interested I went with a processor.. Processor name and parameters set to "EX".


Code follows..



// remotely execute a scheduled job, restricted to admin only


(function(){


  if(!gs.hasRole('admin')) return;


  var name = g_request.getParameter("name");


  var scriptname = new GlideRecord('sysauto_script');


  scriptname.get('name', name);


  if(!scriptname) return;        


  SncTriggerSynchronizer.executeNow(scriptname);


  g_processor.writeOutput('','ok ');


})();



call like so https://instance.service-now.com/?EX&name=<script>