ServiceNow: run an asynchronous script from a business rule
I assume that you already know about asynchronous business rules and that they don't work for you, because you need to do something synchronously too.
You can use GlideRunScriptJob
from script to execute your script on a background thread. If you already have a script, you simply pass it as a string to a method scheduleScript
in GlideRunScriptJob like so:
var job = new GlideRunScriptJob();
job.scheduleScript("new CustomScriptInclude().foo()");
source to share
You can also do this with script events and actions. So in the business rule, you create an event and attach a script action to that event. The script will run when the event is processed.
If you want to run it in the future, you can use the gs.eventQueueScheduled function to schedule an event to be fired in the future:
gs.eventQueueScheduled (event.name, current, parameter1, parameter2, data and time to fire the event);
source to share