Trying to set a delay in the script

Ella1
Kilo Expert

I am trying to set a delay in the script and after 1 sec I want the queries to run, but I am unable to see the logs and the code is not executing at all line 64 log is not visible in the logs
find_real_file.png

8 REPLIES 8

Anurag Tripathi
Mega Patron
Mega Patron

Hi

You can use eventQueueScheduled as gs.sleep is also an option but it is not recommended

var when = new GlideDateTime();

var delay = new GlideTime();

delay.setValue("00:00:05");

when.add(delay);

gs.eventQueueScheduled("event_name", current, "", "", when);

 

gs.sleep works as below

gs.sleep(<time in milliseconds>)

 

Also, check this out

https://community.servicenow.com/community?id=community_article&sys_id=2bada4a2db9bf30014d6fb243996193b

 

-Anurag

-Anurag

Harish K
Kilo Contributor

Hi Ella,

 

The easiest solution is to try gs.sleep(milliseconds); 

 

If you will re-use it too often, put the below function in a script include and you can call it wherever required with the required delay in milliseconds.

sleep: function(duration) {
        gs.sleep(duration);
    },

 

Regards,

Harish

Tony Chatfield1
Kilo Patron

Hi, adding any sort of delay in a script can result in\contribute to degraded platform performance.
In most circumstances delays are added as an attempt to work around base platform functionality,
and a better approach is to develop solutions that are aligned with the way the platform functions\operates.

Hitoshi Ozawa
Giga Sage
Giga Sage

I've tried to put a delay in the script but found it doesn't work too well.

The solution I ended up doing was not to put a delay but to create a scheduled job that'll create an event to trigger an action after a duration.