As most of us are well aware, ServiceNow offers a great variety of scheduled job options in the system. Say you want to clean up a table every Tuesday at 3AM, you just write a simple script and it executes every Tuesday at 3AM. However, we live in a world where ServiceNow is not the only system calling the shots in an organization. What if you had a script that was dependent on a third party application completing one of its jobs and you want that system to kick of a job in your ServiceNow instance.

There are actually several ways you could do this. For one, you could create a scripted web service and have your third party system send a SOAP call to ServiceNow and have it kick off. You can see how to build scripted web services via my Static WSDL and Scripted Web Service tutorial.

For this demo, however, we are going to choose a more REST-like approach. We are going to create a ServiceNow page, that when accessed with the proper credentials, rights, and URL parameters, will launch a scheduled server-side script.

Create the Scheduled Job

Browse to “Scheduled Jobs” in the “System Definition” application. There are a variety of schedule jobs links in the navigation bar, but for this tutorial, choose the once under “System Definition”. Once you see the scheduled jobs, click the “New” button to create a new scheduled job script.

When you click “New” you will be presented with some Scheduled Job options, choose the option, “Automatically run a script of your choosing”.

Give your job a name. In this example, I named our script: “Log Hello”. I set it to run Monthly on the first day of the month (You could set this to run on demand only, or anything else for that matter). The script itself does nothing useful but logs a “Hello” message to the system log. However, this is where you would write the script you wanted to execute.

Create a Processor

As I mentioned, you could launch this script through a number of ways. A few examples include a Scripted Web Service, or a UI Page, but we are going to use a Processor.

Browse to the Processors in the system and click the “New” button to create your own processor.

Now, I am going to give my Processor a name of “Launch Script”. Also, there are two types of processors, Java and Javascript. A Java processor executes java code from the ServiceNow build itself. We don’t have the ability to write to that, so we are going to execute a Javascript “Script”. So, choose the “script” option.

Now the “Path” field tells us that we will access this processor via a URL path. I named mine: “launch_scheduled_script”. To access this processor, I request the following URL:

https://myinstance.service-now.com/launch_scheduled_script.do

It is import to name your processor path something that won’t conflict with a table name, ui page, etc already in your system.

Now, I build the magical script that verifies rights and handles the launching of the Scheduled Job.

My code first checks to see if you have the correct roles as a user. This is a protected page in the system, so your third party program that requests this processor will need to authentication via Basic Authentication, and those credentials will need to have the proper roles to execute the script. We define those roles at the very first part of our script.

Next, we want this processor to launch any scheduled job in the system. So, we will require that the processor is called with a URL paramter of “sysparm_sys_id”. That parameter should contain the sys_id of the scheduled job we want to execute.

If we can find that scheduled job, we execute it and return the trigger ID that was generated for the job to execute.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// First, check to see if the user has rights to execute this script
// This examples uses Admin, but you could have any role of your choice
if (gs.hasRole("admin")) {
  executeScript();
} else {
  g_processor.writeOutput("ERROR: Insufficient rights to execute");
}

function executeScript() {
  var sys_id = ""+g_request.getParameter("sysparm_sys_id");
  if (!sys_id) {
    g_processor.writeOutput("ERROR: No sysparm_sys_id parameter was found");
    return;
  }
  var script = new GlideRecord("sysauto_script");
  var res = script.get(sys_id);
  if (res) {
    var res = Packages.com.snc.automation.TriggerSynchronizer.executeNow(script);
    g_processor.writeOutput("Script executed and returned trigger ID: " + res);
  } else {
    g_processor.writeOutput("ERROR: Could not find scheduled job: "+sys_id);
  }
}

The Scheduled Job that I want to execute has a “Sys_ID” of: 94d53cf494146000e2abe5e53b9398f5

So, in order for me to launch that script, I simply need to browse to:

https://myinstance.service-now.com/launch_scheduled_script.do?sysparm_sys_id=94d53cf494146000e2abe5e53b9398f5

If everything is successful, I get the following response: