Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

gs.sleep() in scoped application?

Artemis15
Kilo Guru

Hi,

gs.sleep(0 function is not working in scoped application?   Is there any workaround?

5 REPLIES 5

David Patterson
ServiceNow Employee

Use of gs.sleep() in ServiceNow server-side scripts is not recommended, because it keeps the program's thread active for the entire duration, and can eventually deplete execution resources on the system if it is re-executed many times in parallel.  However for very short-duration waits, use of gs.sleep() might be an acceptable solution.

To make this work from within a scoped app, you'll need to create a small utility script that is a) global scope, and b) accessible from all scopes.  E.g.:

var SleepForScopedApp = Class.create();
SleepForScopedApp.prototype = {
    initialize: function(debug) {
        this.debug = debug;
    },
    /**
     * Sleep for the provided number of seconds.
     */
    sleep: function(secs) {
        this.debug && gs.info("Sleeping for " + secs + " secs");
        // gs.sleep() parameter is in msecs
        gs.sleep(secs * 1000);
        this.debug && gs.info("Done sleeping");
    },
    type: 'SleepForScopedApp'
};