Unlocking Client-Side Efficiency: Understanding the ServiceNow Scratchpad (g_scratchpad)
- nathanlee142
- Mar 21
- 4 min read
Updated: Mar 28

In the world of ServiceNow development, creating efficient and responsive user interfaces is paramount. Often, client-side scripts need access to server-side data to perform validations, make dynamic changes, or personalize the user experience. While various methods exist for retrieving server-side information on the client, making frequent server calls can impact performance. This is where the ServiceNow scratchpad, specifically the g_scratchpad object, comes into play, offering a streamlined way to share data from the server to the client and optimize your ServiceNow forms.
Let's demystify the ServiceNow scratchpad and explore how it can enhance the performance and responsiveness of your ServiceNow applications.
What is the ServiceNow Scratchpad (g_scratchpad)?
The g_scratchpad is a temporary, client-side JavaScript object in ServiceNow that acts as a container for data passed from the server to the client. Think of it as a small notepad where server-side scripts can jot down information that client-side scripts can then readily access. The primary purpose of the scratchpad is to avoid unnecessary server-side calls from client scripts, thereby improving form load times and overall application performance.
How Does it Work? The Role of Display Business Rules
The magic behind the g_scratchpad happens during the form loading process. When a ServiceNow form is requested, the platform first retrieves the necessary data from the database. At this point, Display Business Rules come into play. These are server-side scripts that execute before the form is rendered on the client's browser. Within a Display Business Rule, you can populate variables within the g_scratchpad object with any server-side data your client-side scripts might need.
Once the form is loaded on the client, all client-side scripts (like Client Scripts, UI Policies executing scripts) can access the data stored in the g_scratchpad object. While the g_scratchpad object is primarily used to pass data from the server to the client, it is not read-only on the client-side. Client scripts can modify its values, but these modifications do not persist back to the server automatically. If you need to send updated data back to the server, you must use GlideAjax or other server-side interaction methods.
The Performance Advantage: Reducing Server Round Trips
Imagine a scenario where you need to perform a client-side validation based on whether the current user has a specific role. Without the g_scratchpad, your client script would likely need to make an asynchronous call back to the server (using GlideRecord or GlideAjax) to fetch this role information. This adds an extra round trip to the server, which can slow down the user experience, especially if multiple such checks are needed.
The g_scratchpad elegantly solves this problem. By using a Display Business Rule, you can fetch the user's roles once when the form initially loads and store them in g_scratchpad. Your client-side validation script can then directly access this information from the g_scratchpad without needing to make any additional server calls.
Practical Example: Validating VIP Users on Incident Forms
Let's consider a common use case: displaying an alert if the caller on an Incident form is a VIP user.
Display Business Rule (Server-Side): You would create a Display Business Rule on the incident table with the following script:
JavaScript
(function executeRule(current, previous /*null when async*/) {
g_scratchpad.isVIP = current.caller_id.vip;
})(current, previous);
This script runs when the Incident form is displayed. It fetches the value of the vip field from the caller_id (which is a reference to the User table) and stores it in a g_scratchpad variable named isVIP.
Client Script (Client-Side): You would then create an onLoad Client Script on the incident table with the following script:
JavaScript
function onLoad() {
if (g_scratchpad.isVIP == 'true') {
alert("Caller is a VIP user");
}
}
When the Incident form loads, the Display Business Rule executes first, populating g_scratchpad.isVIP. Then, the onLoad Client Script runs and can directly access the value of g_scratchpad.isVIP to determine if the alert should be displayed. Notice that the Client Script did not need to make a separate server call to get the VIP status.
Key Benefits of Utilizing g_scratchpad
Improved Performance: Significantly reduces the number of server-side calls from client scripts, leading to faster form loading and a more responsive user experience.
Efficient Data Sharing: Provides a simple and effective way to share necessary server-side data with client-side logic.
Optimized User Experience: Faster loading times and reduced delays contribute to a smoother and more efficient user interaction with ServiceNow forms.
Common Use Cases for g_scratchpad
Beyond the VIP user example, g_scratchpad is useful in various scenarios, including:
Passing user-specific information like roles, group memberships, or preferences to tailor the client-side interface.
Transferring configuration settings or system properties to client scripts for dynamic behavior.
Making server-calculated values (e.g., the result of a complex calculation in a Business Rule) available for client-side display or validation.
Providing context-specific information to client scripts based on the record being viewed.
Important Considerations
While g_scratchpad is a powerful tool, remember that it is primarily for passing data from the server to the client. It is read-only on the client side. If you need to send data back to the server from a client script, you would typically use methods like GlideAjax or submit the form.
Conclusion
The ServiceNow g_scratchpad is an invaluable tool for developers looking to optimize the performance of their client-side scripts. By strategically using Display Business Rules to populate the g_scratchpad with necessary server-side data, you can significantly reduce the number of server calls and create more responsive and efficient ServiceNow applications. Embrace the power of the scratchpad to enhance your users' experience and build better ServiceNow solutions. As a next step, identify areas in your ServiceNow instance where client-side scripts are fetching server-side data and explore if using g_scratchpad could streamline the process and improve performance.