Whats the best way to schedule a script to run in 2 minutes on the fly as it were?

DrewW
Mega Sage
Mega Sage

I have a script that I need to run on occasion.   In certain circumstances I need to run another piece of the code but I need there to be a delay so that any events triggered by the code before are processed first.   So what I'm looking for is an easy way to run a delayed impromptu script and when I say delayed I mean by several minutes.   I know I can create a record in sys_trigger but it just seams to me with GlideRunScriptJob there should be a way to feed it or something else the script code and a date/time to run it at.

Anyone have any thoughts?

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Consider using gs.eventQueueScheduled() to tell it what time to trigger the event and then put your code in a script action.



Avoid using the gs.sleep() method as it has been deprecated and can tie up system resources while it waits for the timer to expire (definitely a bad practice.)



If you are using a workflow, you can use a Timer activity.


View solution in original post

5 REPLIES 5

Chuck Tomasi
Tera Patron

Consider using gs.eventQueueScheduled() to tell it what time to trigger the event and then put your code in a script action.



Avoid using the gs.sleep() method as it has been deprecated and can tie up system resources while it waits for the timer to expire (definitely a bad practice.)



If you are using a workflow, you can use a Timer activity.


Hey Chuck, thanks for the suggestion, I had forgotten that was there.   But since there is no documentation for it how could I search to remember....



In the case that I'm currently working this will work, but I have another one where the script will be different each time it needs to run.


For something that is a different script each time consider creating your own scheduled job. You'll likely want to clean the records up at some point if you're running lots of them.



http://wiki.servicenow.com/index.php?title=Creating_a_Scheduled_Job#gsc.tab=0


Thats basically what I decided to do I just was hoping there was a way I could be lazy and just call a pre-built object.   But alas no such luck.