The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Question regarding adding delay in a script

nirwan_ritik
Tera Contributor

Hi Community,

 

https://www.servicenow.com/community/developer-forum/need-delay-function-for-script/m-p/1409922

I recently stumbled across this article where @Ankur Bawiskar has suggested a way to incorporate a delay in a script. Tried it, and it is working as expected. But, few doubts not leaving from my mind.
1. Is this the correct approach?
2. Should we use this in an actual implementation (if there's no other way to satisfy the requirement)?
3. Impact on performance?

4. Any alternatives?

 

Awaiting response from community as well as Ankur 🙂

Regards,
Ritik

6 REPLIES 6

VikMach
Mega Sage

@nirwan_ritik, what was the use case for which you added delay in script? If you provide the complete context of what it was all about may be community can think.
Please don't post questions with suspense.
We are not Albus Dumbledore. Jk...

Regards,
Vikas K

@nirwan_ritik, I see ServiceNow developers have used "sleep" in several Script Includes. Pasting a few here for your reference.
This looks okay because if there is a reason to delay the execution of the particular code or function to establish something then you can't help it. See some snips from OOB script includes I have pasted below. It all depends on use case.
Alternate method  - You can also call the script include in any other place if you ever see any issues in performance. For example, scripted Scheduled Jobs, Flow Designer Action etc... but use it wisely and sparingly.
By the way if it was really impacting performance, ServiceNow Dev's won't use it and sell the product to their customers worldwide. (Would cause serious issues brah!)
Some examples of how and where ServiceNow Dev's used gs.sleep in their code. You can open them in your instance and check the use case for which they used it.

VikMach_0-1756477517158.png



VikMach_1-1756477627681.png



VikMach_2-1756477656046.png



VikMach_3-1756477679024.png



VikMach_4-1756477701616.png



VikMach_5-1756477726197.png


Hope this helps!

Regards,
Vikas K

Ankur Bawiskar
Tera Patron
Tera Patron

@nirwan_ritik 

ServiceNow do use sleep in many of the OOTB scripts so it should be fine to use.

It varies from customer to customer and based on requirement as well.

yes it will have some performance impact as you are making system wait on particular thread for particular timeframe.

what's your business requirement to use delay?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader