- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2024 04:13 AM
Hi,
I want a delay of 10 seconds in my scripted REST API Script.
It is working through gs.sleep() - my usecase is to wait for sc_task to be generated after creation of RITM.
But, since this is not a best practice to use gs.sleep() , I want an alternative approach to implement.
Can anyone help on this?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2024 04:31 AM
Hi @mansigoel ,
You can use:
var startTime = new Date().getTime();
var delay = 10000; // Simulating a delay of 10 seconds
while (new Date().getTime() < startTime + delay) {
// Simulating the delay
}
If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.
Thanks,
Amitoj Wadhera
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2024 04:27 AM
Hi @mansigoel
refer to the below link for waiting response.
Thanks
dgarad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2024 04:31 AM
Hi @mansigoel ,
You can use:
var startTime = new Date().getTime();
var delay = 10000; // Simulating a delay of 10 seconds
while (new Date().getTime() < startTime + delay) {
// Simulating the delay
}
If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.
Thanks,
Amitoj Wadhera
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2025 10:22 AM
I recommend against a solution that would freeze threads (e.g. while loop or gs.sleep). ServiceNow instances have a discreet number of threads available to handle incoming requests (these are known as Semaphore Pools). If you hang the threads doing pointless loops or sleep operations then you are wasting scarce resources that could be used to handle other incoming integration traffic. In the worst case, you could completely use up all your semaphores and any new requests would be stuck until threads are available.
Instead you should look at asynchronous implementation methods, for example, you could use the events queue or scheduled job methods to schedule some operation to happen 10 seconds in the future.
https://www.servicenow.com/community/developer-articles/custom-event-queues/ta-p/3045097
It is undocumented, but something I've done in the past is to directly insert records into the sys_trigger table with the "next_action" field set to some time in the future. This has the advantage of leveraging the 8 scheduled worker threads and potentially clogging the Default event queue (you can make custom event queues as noted above, but that is an extra step).
Here's an example:
var script = "script here";
var nowGDT = new GlideDateTime();
nowGDT.add(1000); // add 10 seconds to the date time
var job = new GlideRecord("sys_trigger");
job.initialize();
job.setValue("trigger_type", 0);// 0 is run once, 2 is ondemand
job.setValue("script", script);
job.setValue("next_action", nowGDT);
job.setValue("name", "testName");
job.insert();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2024 04:56 AM
Hi @mansigoel,
Please explain in detail.
- Is third party system calling your API and you want to delay the response for 10 secs? Delaying API response is not advisable. We should
- Or you are calling an API from Workflow or BR? Please explain how you are calling them.
Palani