- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 07-04-2019 03:01 PM
For ATF Testing (and other requirements) it might be necessary to execute scheduled jobs immediately.
Schedule Jobs can be Executed by using a a server side script.
For ATF Testing this would be a Server Script Test Step:
Here is some sample code. I am new to ATF - so I hope this is useful.
current = new GlideRecord('sysauto_script');
current.addQuery('name','Schedule Job Name');
current.query();
if (current.next()) {
// current.update();
SncTriggerSynchronizer.executeNow(current);
gs.log("Successfully executed the Scheduled Job");
//return true;
} else {
gs.log("Failed to find the Scheduled Job");
//return true;
}
- 5,422 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
2 things I would change:
1. change the variable name from "current" to "gr" (or something else) to avoid confusion
2. wrap the code in an Immediately Invoked Function Expression (IIFE) to protect it from the global scope and vice versa
(function(){
var gr = new GlideRecord("sysauto_script");
gr.addQuery("name", "Scheduled Job Name");
gr.query();
if (gr.next()) {
SncTriggerSynchronizer.executeNow(gr);
gs.log("Successfully executed the Scheduled Job");
} else {
gs.log("Failed to find the Scheduled Job");
}
})();
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
And, per best practices, don't use 'gr' 🙂
Yes, with the IIFE it doesn't matter so much, but I believe various instance health checks will still flag it.
P.S. Yes, still useful in 2024
(and not just for ATF, we're setting up a Scripted REST API that will take a payload and format parts of it into an Import Set table and then kick off the import using code similar to this).
P.P.S. ServiceNow now(?) has documentation on their website that skips the query and just goes straight with a get:
//Execute a scheduled script job
var rec = new GlideRecord('sysauto_script');
rec.get('name', 'YOUR_JOB_NAME_HERE');
SncTriggerSynchronizer.executeNow(rec);
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Yes, "good practice" (don't like the term "best" practice) would dictate not using "gr" as a variable name for GlideRecords. I've switched to using more descriptive names: "job" would seem to be a good one in that particular case. You'll still get a lot of arguments on this, but why run the risk. 🤓
The funny part, however, is the use of ServiceNow code as an example. The platform is littered with OOTB records with poor coding or examples in their documentation that make no sense. While the "get" is simpler, and I've switched to using that style more and more now, in that particular example:
- "rec" means nothing, no better than "gr". Should be using meaningful variable names
- there's no verification the record was found first before executing the job
The code should be more like:
//Execute a scheduled script job
var job = new GlideRecord("sysauto_script");
if (job.get("name", "YOUR_JOB_NAME_HERE") {
SncTriggerSynchronizer.executeNow(job);
} else {
//some error messaging here probably makes sense
}
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
100% ! (especially the bit about the issues in the OOTB code 🙂 )