Sleep function in server side script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-04-2017 07:51 AM
In my server side script, I am triggering an action via an outbound ReST call. The asynchronous operation returns immediately, but provides a URL which I can poll for the operation's completion. I would like to keep the function atomic, so would like to poll the URL, and if the operation hasn't yet completed, would like to wait for a second or so and poll again, and do that for several times (I do not expect the operation to take more than 10-20 seconds, normally not more than 5 seconds) until done.
I am looking for something like setTimeout or setInterval function that I could use on the server, so that I can tell my script to wait, but can't find anything. Am I missing something? Can somebody point me to a solution for this kind of problem?
I have seen a timer activity in Workflow, which would pause the workflow for a specified period of time; it can probably serve as a workaround, but I wanted a programmatic (and programmable) solution. Will be grateful for any guidance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2023 01:21 PM
It's been a while since I posted this question, but yes, an event is exactly what I settled on. It required some re-thinking of the workflow, but proved to be a better solution at the end.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2024 01:52 AM
In ServiceNow, there is no built-in sleep or delay function for server-side scripts. This is because server-side scripts are designed to execute as quickly as possible and return a result. Adding a sleep or delay function could potentially slow down the server and degrade performance. However, if you really need to introduce a delay in your server-side script, you can create a loop that runs for a certain amount of time. Here's a simple example: javascript var start = new GlideDateTime(); start.setDisplayValue('01-01-2013 09:00:00'); // Set start time var end = new GlideDateTime(); end.setDisplayValue('01-01-2013 09:01:00'); // Set end time while(start.getNumericValue() < end.getNumericValue()) { start.addSeconds(1); // Add one second to start time } This script will run for approximately one minute. Please note that this is not a recommended practice and should be used sparingly, if at all. To summarize: - ServiceNow does not have a built-in sleep or delay function for server-side scripts. - Server-side scripts are designed to execute quickly and adding a delay could degrade performance. - If necessary, you can create a loop that runs for a certain amount of time to introduce a delay. - This is not a recommended practice and should be used sparingly, if at all. nowKB.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2024 04:33 AM
In ServiceNow, there is no built-in sleep or delay function for server-side scripts. This is because server-side scripts are designed to execute as quickly as possible and return a result. Adding a sleep or delay function could potentially slow down the system and degrade performance. However, if you really need to introduce a delay in your server-side script, you can use a workaround using GlideRecord query inside a loop. Here is a sample code: javascript var gr = new GlideRecord('table_name'); gr.query(); while(gr.next()) { // your code here for (var i = 0; i < 10000; i++) { var x = i; } } In this code, the for loop doesn't do anything but it takes time to execute, effectively introducing a delay. Please note that this is not a recommended practice and should be used sparingly and only when absolutely necessary. Summary: - ServiceNow does not have a built-in sleep or delay function for server-side scripts. - Server-side scripts are designed to execute quickly and adding a delay can degrade performance. - A delay can be introduced using a loop with a GlideRecord query. - This is not a recommended practice and should be used sparingly and only when necessary. nowKB.com