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)
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);
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.
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(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;
}
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).
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
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
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
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
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
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.
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
@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.
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!!!
Thanks for the info, I’ve been looking for this syntax on the SN wiki without any luck.
No problem. I’m glad it helped!
Awesome mate
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);
Thanks Travis! Great tip!
That tip ! Works like a charm !