top of page

Experiencing challenges with ServiceNow support?

Access professional, reliable assistance tailored to your needs—without breaking your budget.

Using gs.sleep() in Business Rules: Best Practices for ServiceNow Developers

Updated: Mar 30

When working with ServiceNow's powerful platform, developers often explore ways to control execution timing within business rules. One common method mentioned by developers is gs.sleep(). However, while it may seem straightforward, using gs.sleep() in business rules can lead to unexpected issues, particularly delays in form submission and overall system performance problems. This article clarifies the implications of using gs.sleep() in ServiceNow business rules, identifies common pitfalls, and provides recommended alternatives to maintain system efficiency and reliability.


Understanding the Impact of gs.sleep() in Business Rules

The gs.sleep() method pauses the execution of server-side scripts for a specified duration, typically measured in milliseconds. For example, gs.sleep(5000) pauses the execution for 5 seconds. Although this might seem beneficial for ensuring certain actions complete before subsequent code runs, using gs.sleep() within business rules—particularly in "after" business rules—can directly delay user-facing processes like form submission.

Here's why using gs.sleep() is problematic:

  • Delayed User Experience: Placing a sleep within a business rule causes the entire submission process to halt until the sleep duration completes, leading to noticeable delays and a frustrating experience for end users.

  • Performance Impact: Frequent or lengthy sleeps can severely degrade system performance, consuming resources unnecessarily and potentially leading to timeouts or resource contention.

  • Process Blocking: Since business rules run synchronously by default, the entire operation must wait for the sleep interval, potentially impacting other critical system processes.


Best Practice Alternatives to gs.sleep()

Instead of using gs.sleep(), it is advisable to leverage ServiceNow's event-driven architecture to handle delayed or scheduled execution more efficiently and asynchronously. Here's a reliable approach:


Using Scheduled Events

Utilizing scheduled events provides a clean, maintainable, and efficient solution to manage delays without blocking execution.


Step-by-Step Guide:

  1. Register a Custom Event

    • Navigate to the Event Registry and create a new event, e.g., custom.metric_update.

  2. Update Business Rule

    • Replace your gs.sleep() statement with code that schedules this event to trigger after your desired delay:

var scheduleTime = new GlideDateTime(); 
var delayDuration = new GlideTime(); 
delayDuration.setValue("00:00:05"); // 5-second delay scheduleTime.add(delayDuration);

gs.eventQueueScheduled("custom.metric_update", current, "", "", scheduleTime);
  1. Create a Script Action

    • Under Script Actions, associate a new action with your registered event (custom.metric_update). Implement the logic you originally had within your business rule here:

(function executeMetricUpdate(event) { 
	var metricGR = new GlideRecord('metric_instance'); 
	metricGR.addQuery('id', event.instance); 
	metricGR.query(); 
	if (metricGR.next()) { 
		metricGR.previous_value = event.parm1; 
		// Assuming you passed previous value
		metricGR.update();
	} 
})(event);

Example Use Case:

Suppose your requirement is to update a Metric Instance record shortly after an Incident is updated. Using the steps above, the incident updates trigger a scheduled event, which then asynchronously updates the Metric Instance record after a short delay, without affecting the immediate user experience.


Alternative Approaches

  • Asynchronous Business Rules: Another effective solution is implementing asynchronous business rules. They run independently of user-facing actions, thereby preventing UI delays.

  • Workflows or Flow Designer: Leveraging ServiceNow's workflows or the Flow Designer provides visual and efficient management of processes involving timed actions and event-driven logic.


Conclusion

While the simplicity of gs.sleep() might be tempting, it poses significant risks to user experience and system performance within ServiceNow. Adopting event-driven programming using scheduled events, asynchronous business rules, or Flow Designer workflows offers more robust, maintainable, and scalable solutions. By moving away from blocking operations like gs.sleep(), developers can ensure a smooth, responsive, and efficient experience for users.


Actionable Next Steps:

  • Review existing business rules for use of gs.sleep() and plan migration to recommended alternatives.

  • Familiarize yourself with ServiceNow’s event registry and script actions.

  • Leverage ServiceNow Flow Designer to simplify process automation and manage timed actions effectively.

Experiencing challenges with ServiceNow support?

Access professional, reliable assistance tailored to your needs—without breaking your budget.

CONTACT

New Zealand HQ

Integrated Knowledge Consulting Office

Level 3, 93 Grafton Road

Auckland

South Korea

Integrated Knowledge Consulting Office

BMY역삼타워 6층

서울특별시 강남구 역삼동 678-10번지

 

info@ikconsulting.com

Thanks for submitting!

  • LinkedIn Social Icon

© Copyright 2025 Integrated Knowledge Consulting. All rights reserved.

bottom of page