Scheduled Events and Script Actions

Published Categorized as ServiceNow Tagged , , , , ,

There are various methods to execute server-side logic in ServiceNow, with business rules and script includes being among the most common. However, when there’s a need to delay the execution of logic for a specified period, the options become limited. In such scenarios, script actions combined with scheduled events can be particularly effective. Despite their utility, script actions remain one of the most underrated features in ServiceNow.

What are script actions?

A Script Action [sysevent_script_action] is a server-side script that executes in response to the trigger of a specified event in the Event Registry [sysevent_register] table. It allows for business logic to be applied when the event is processed in the Events [sysevent] table.

What are scheduled events?

Scheduled events are similar to regular events but are distinguished by having a predefined date and time for their trigger.

  • Normal event, when added to the event queue gets processed within seconds. Syntax –
gs.eventQueue(name, recGr, parm1, parm2, eventQueue);
  • Whereas when a scheduled event is added to the event queue, it waits until the given date and time to get processed. Syntax –
gs.eventQueueScheduled(name, recGr, parm1, parm2, gdt);
  • Here ‘gdt’ is a GlideDateTime object.

Example

Let’s assume that there is a requirement to introduce a 5-minute delay within the logic of a script include.

var scriptIncludeName = Class.create();
scriptIncludeName.prototype = {
    initialize: function() {},

    functionOne: function(parameters) {

		/*
		some logic
		*/

		// wait 5 minutes

		/*
		some more logic
		*/

    },

    type: 'scriptIncludeName'
};

Can’t really make the script wait synchronously, so the idea would be,

  • split the logic into two separate functions
  • schedule an event at the end of first function
  • event gets processed after 5 mins and triggers a script action
  • script action calls the second function to execute

Register event

Register an event that can be queued from the script include and can later call the script action.

Modify script include

var scriptIncludeName = Class.create();
scriptIncludeName.prototype = {
    initialize: function() {},

    functionOne: function(parameters) {

        /*
        some logic
        */

       var fiveMins = new GlideTime();
       fiveMins.setValue("00:05:00");
       var gdt = new GlideDateTime();
       gdt.add(fiveMins);
       gs.eventQueueScheduled(waitEvent, recGr, parm1, parm2, gdt);

    },

    functionTwo: function(parameters) {

        /*
        some more logic
        */

    },

    type: 'scriptIncludeName'
};

Add script action

Event name – the event that is being triggered by functionOne

Script –

/*
event.parm1 and event.parm2 are accessible
some logic to process the event data
some logic to prepare input parameters for the function call
*/

new scriptIncludeName().functionTwo(parameters);

Result

We have successfully implemented a 5-minute delay between the execution of two script include functions. The delay is asynchronous in nature and would not block any system resources.

Next steps

Many problems can be solved more efficiently by utilising such available system artefacts. For instance, you can address a scenario by recursively calling the same function with a delay until a condition is met.

  • functionOne executes
  • condition not met
  • event triggered
  • wait for time
  • script action triggered
  • functionOne executes

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses User Verification plugin to reduce spam. See how your comment data is processed.