THURSDAY, JANUARY 19, 2017

Execute Scheduled Jobs From Script

Scheduled jobs are an extremely useful way to automate processes in Service-now.com and lift some of the administrative burden of the tool off of the shoulders of your Service-now administrators. It’s very easy to create a scheduled job or a scheduled import. Typically there’s no scripting or advanced configuration involved at all. Just set it up with a schedule and let it run! There are some situations where you might need to execute a scheduled job outside of its normal schedule. You can always open up the scheduled job entry and click the ‘Execute Now’ button, but there are also probably times where you’d really like to automate the process based on some trigger in a workflow or state change in some ticket or CI.

This article shows you how you can use script to execute an already-scheduled job on-demand. To do this, you simply mimic the behavior of the ‘Execute Now’ button by querying for the scheduled job you want to execute and firing off the job. Here’s the script…

You can modify the script below by adding your scheduled job name and querying the appropriate table for your scheduled job as shown here. This will not work if you simply query the ‘sysauto’ table. You must query one of the extensions below directly.

  • ‘scheduled_import_set’ (Scheduled Import Sets)
  • ‘sysauto_script’ (Scheduled Script Execution)
  • ‘sysauto_template’ (Scheduled Template Generation)
  • ‘sysauto_report’ (Scheduled Report)
//Execute a scheduled script job
var rec = new GlideRecord('sysauto_script');
rec.get('name', 'YOUR_JOB_NAME_HERE');
if (typeof SncTriggerSynchronizer != 'undefined')
   SncTriggerSynchronizer.executeNow(rec);
else
   Packages.com.snc.automation.TriggerSynchronizer.executeNow(rec);

17 Comments

Dave Richter 09-12-2010, 03:15

I have over 20 scheduled data imports, each one associated with an LDAP ou definition, each pair named the same.

I have a post-import script in one of the 20+ that updates the date in the LDAP filter successfully, but the LDAP ou definition name is hard-coded. I want to use a script that reads the current scheduled_data_import name instead of hard-coding that name, then copy that in all 20+. However when I use “current.variable” it keeps failing because its null. There must be a universal way to pull the “current” record. (current at the time I execute the script. Thank you.

Reply
Mark Stanger 09-12-2010, 05:24

I don’t think you can get around hard-coding in that script field. There is a way you can cheat though so that you can dynamically pull values from the ‘current’ record for your script…and only need one script to do it all.

You can create a ‘before’ business rule on the ‘Scheduled Data Import’ table that will check to see if the scheduled import job meets the correct criteria (LDAP source, Execute post script checked, etc.). The business rule would run before updates to the scheduled import record and populate the ‘Post Script’ field for you based on the ‘current’ scheduled import set record. This way, the ‘Post Script’ field can be hard coded as it needs to be, but it won’t truly be hard-coded because your business rule will pull the dynamic values and populate them for you. Here’s a sample script that should get you started on your business rule if you want to give this a shot.

//If we're working with an LDAP data source and Executing a post-import script
if(current.data_source.type == 'LDAP' && current.post_script_bool == true){
   //Generate a script string to be populated in the 'Post script' field
   var s = '';
   s += "//Do something here..."
   s += "var rec = new GlideRecord('ldap_ou_config');"
   s += "rec.addQuery(field, value);"
   s += "rec.addQuery('field2', '" + current.name + "');";
   s += "rec.query();"
   s += //Continue here...
 
   //Set the 'Post script' field with the script string
   current.post_script = s;
}
Reply
Brian Broadhurst 05-01-2011, 07:05

The ability to trigger scheduled jobs from scripts is really useful – one of the most useful applications I have found is being able to email a spreadsheet into the system, then trigger a scheduled import job from an email inbound action to import the spreadsheet (see forum topic http://community.service-now.com/forum/2440).

I’ve also had a requirement recently that I’ve been experimenting with “daisy-chaining” these triggers – a customer has a complex report that uses data from a number of tables – one approach we are looking at is to create a custom table and a scheduled script that reads the relevant tables and populates this table with the data required in the report. The idea is that a UI Action will trigger the scheduled script which populates the custom table then triggers a scheduled report which generates a report from the custom table and emails it to the user. Still in the early stages of experimentation.

BTW I believe you can also trigger a scheduled Custom Chart using the sysauto_custom_chart table – I’ve never tried this (never got to grips with custom charts I’m afraid).

Reply
Ajay 04-02-2011, 10:28

Hi,

I am looking for a feature like we have some security, IT and some other DB groups where they have to do some task every month or for 45days or some specific days, is there a way where we can do something like schedule the job and give the time to run for every 30 or 45 days, the system should be able to run that on the mentioned date and create a Incident and assign it to the specific group????

how to do that could you please help me..

thanks

Ajay

Reply
Mark Stanger 04-02-2011, 10:36

Check out Scheduling Entity Generation on the Service-now wiki. It allows you to create a task record (which you can populate with a template) based on any schedule you want. No scripting necessary.
http://wiki.servicenow.com/index.php?title=Creating_a_Scheduled_Job#Scheduling_Entity_Generation

Reply
Wes Van Sickle 07-02-2011, 07:30

We have a need for a Scheduled Job that will create a Facilities Request every 180 days for a Preventive Maintenance task. I know how to create the scheduled job that will initiate this request every 180 days, but I was wondering if Service Now had the capability to begin the 180 day countdown ONLY after the last auto generated ticket was closed ?

Any assistance would be greatly appreciated.

Thanks,

Wes

Reply
Mark Stanger 07-02-2011, 08:04

Hey Wes,

This is possible and would probably be best accomplished by setting up some sort of business rule trigger that would actually create a scheduled job record to run once – 180 days in the future. I don’t have a pre-built script for that, but it should just be a GlideRecord insert into the scheduled job table.

Mark

Reply
Abhiram Bharadwaj Diddigi 03-10-2011, 00:29

Hey Mark,
Just curious , Where do you have all the available packages and their methods ? I always come across some handy methods which use ‘packages.’ , Wanted to know if you have them documented 🙂

Thanks

Reply
Mark Stanger 03-10-2011, 07:56

I don’t have them documented, and actually their use is discouraged by ServiceNow development so you should avoid them whenever possible. In this case, there’s no other way to do it so I used the packages call.

Reply
Dennis Smith 20-12-2011, 07:13

I wanted to see if there is a good client side script that I can use for scheduled reports. I would like to have a script that will check and see if the report contains any data, and if it does, then send as scheduled. If it does not contain any data I would like to have the script stop the scheduled send of that report until the next scheduled time and then to follow the logic again.

For instance a report scheduled to run tomorrow automatically and yet there are no overdue incidents in the report for this scheduled job, the script should say that since there are no overdue incidents this week, the report does not run

Reply
Mark Stanger 20-12-2011, 07:38

@Dennis, I don’t have any script that would do this, but what you are asking for is possible just by checking the ‘Omit if no records:’ field on the scheduled report form.

Reply
Dennis 20-12-2011, 08:14

Thanks Mark, it is funny as you wrote this, I also received another reply from the SN forum for the same thing. Not sure why I did not check the form first. Will start to make this a habit before asking questions. Thanks again Mark and have a great day!!!

Reply
Bryan 03-01-2012, 11:54

Thanks for the info, I’ve been looking for this syntax on the SN wiki without any luck.

Reply
Mark Stanger 03-01-2012, 15:15

No problem. I’m glad it helped!

Reply
MaC 30-11-2015, 20:28

Awesome mate

Reply
Travis 26-10-2016, 07:52

Just to add to this. If you are wanting to do this from a Scoped App. The line:

SncTriggerSynchronizer.executeNow(rec);

needs to be changed to:

gs.executeNow(rec);

Reply
Mark Stanger 26-10-2016, 07:53

Thanks Travis! Great tip!

Reply

Leave a Reply


Latest Comments

  • David: It appears that I can hit sys_properties table with REST. This works, but I haven’t yet discovered the...
  • Mark Stanger: Hey David, It doesn’t surprise me that scoped apps have made this more difficult. I’m not...
  • David: Mark, do you have an example of how to do this in a scoped app? It seems there are many hoops to jump through...
  • Mark Stanger: The only possibility is to create a system property to override this in your application. Check out the...