Streamlining ServiceNow Development: Leveraging g_scratchpad in Business Rules and Workflows
- nathanlee142
- Dec 1, 2024
- 4 min read

In the dynamic world of ServiceNow development, efficiently sharing data across different execution contexts is a common requirement. Whether you're looking to pass information from the server-side to the client-side or transfer data between various stages within a workflow, ServiceNow provides powerful tools to help you achieve this. Among these tools, g_scratchpad and workflow.scratchpad stand out as essential mechanisms for temporary data storage and transfer. Understanding how and when to use these objects can significantly streamline your development process and enhance the functionality of your ServiceNow applications. This article will explore the capabilities of both g_scratchpad and workflow.scratchpad, providing practical examples to illustrate their usage.
While both g_scratchpad and workflow.scratchpad serve the purpose of holding temporary data, they operate in distinct environments and cater to different needs within the ServiceNow platform.
g_scratchpad: Your Server-to-Client Messenger
The g_scratchpad object acts as a conduit for passing information from the server directly to the client's browser. This is particularly useful when you need to make server-side data available to Client Scripts or UI Policies without placing fields directly on the form. Imagine a scenario where a Client Script needs to know if the current user belongs to a specific group to determine the visibility of certain form fields. Instead of making a potentially performance-intensive AJAX call, you can leverage g_scratchpad.
Key Characteristics of g_scratchpad
One-Way Communication: Data flows only from the server to the client. You cannot directly send data back to the server using g_scratchpad.
Availability: g_scratchpad is accessible within Display Business Rules (where you populate it) and Client Scripts or UI Policies (where you retrieve the data).
Efficiency: Using g_scratchpad to provide necessary data to the client can be more efficient than adding hidden fields to the form, as it reduces the amount of data transferred during the initial form load.
Practical Example: Controlling Field Visibility Based on User Group
Let's illustrate this with a common use case: showing or hiding a "Category" field on the Incident form based on whether the logged-in user is a member of a specific support group.
Display Business Rule (on Incident table):
JavaScript
(function executeRule(current, previous /*null when async*/) {
// Replace 'sys_id_of_your_support_group'
// with the actual sys_id of the group
g_scratchpad.isSupportMember = gs.getUser()
.isMemberOf('sys_id_of_your_support_group');
})(current, previous);
This Display Business Rule runs when the Incident form is loaded. It checks if the current user is a member of the specified support group and stores the result (true or false) in g_scratchpad.isSupportMember.
Client Script (on Incident table, onLoad):
JavaScript
function onLoad() {
// Check the value in g_scratchpad
if (g_scratchpad.isSupportMember == true) {
// If the user is a support member, make the Category field visible
g_form.setDisplay('category', true);
} else {
// Otherwise, hide the Category field
g_form.setDisplay('category', false);
}
}
This Client Script runs when the Incident form is loaded on the client-side. It retrieves the value stored in g_scratchpad.isSupportMember and uses it to dynamically control the visibility of the "Category" field.
workflow.scratchpad: Your Workflow Data Repository
Within the context of a ServiceNow Workflow, the workflow.scratchpad object provides a mechanism for storing and sharing data between different activities within that specific workflow execution. This is invaluable for passing values calculated in one activity to be used in subsequent activities.
Key Characteristics of workflow.scratchpad:
Workflow-Specific: The data stored in workflow.scratchpad is only accessible within the scope of the currently executing workflow.
Read and Write Access: You can both store and retrieve data from workflow.scratchpad within various workflow activities.
Data Persistence: The data remains in the workflow.scratchpad for the duration of the workflow execution.
Practical Example: Setting Assignment Group Based on a Routing Table
Consider a scenario where you want to automatically assign a task created within a workflow to a specific group based on a routing table that considers the catalog item and the caller's country.
First Run Script Activity (in your Workflow):
JavaScript
// Find the appropriate assignment group from the routing table var req_group = '';
var gr = new GlideRecord("your_routing_table_name"); gr.addQuery('u_catalog_item',current.variables.your_catalog_item_variable)
gr.addQuery("u_country", current.variables.caller_id.country);
gr.query();
if (gr.next()) {
req_group = gr.u_assignment_group_field;
// Replace with the actual field name
} // Store the assignment group in the workflow scratchpad workflow.scratchpad.assignmentGroup = req_group;
This Run Script activity queries a custom routing table based on the catalog item and the caller's country to determine the appropriate assignment group. The retrieved group's value is then stored in workflow.scratchpad.assignmentGroup.
Second Run Script Activity (or a Create Task activity in your Workflow):
JavaScript
// Example in a Run Script activity before creating a task var taskAssignmentGroup = workflow.scratchpad.assignmentGroup; gs.log("Assignment Group to be set: " + taskAssignmentGroup);
// Example in a Create Task activity (using the 'Assignment group' field) // The 'Assignment group' field can directly
// reference the scratchpad variable:
// javascript:workflow.scratchpad.assignmentGroup;
In a subsequent activity within the same workflow, you can now access the assignment group stored in workflow.scratchpad.assignmentGroup and use it to set the assignment group on a newly created task or for any other relevant purpose.
Key Differences Summarized
Feature | g_scratchpad | workflow.scratchpad |
Primary Purpose | Server-to-client data transfer | Data sharing within a workflow |
Directionality | Server to Client | Read/Write within the workflow |
Availability | Display Business Rules, Client Scripts | Workflow Activities |
Scope | Form load cycle | Specific workflow execution |
Conclusion
g_scratchpad and workflow.scratchpad are invaluable tools in the ServiceNow developer's toolkit for managing and transferring data efficiently. g_scratchpad empowers you to provide necessary server-side information to client-side scripts without unnecessary overhead, leading to more responsive user interfaces. On the other hand, workflow.scratchpad facilitates seamless data flow between different stages of your automated processes within Workflows. By understanding the distinct roles and capabilities of each, you can build more robust, efficient, and user-friendly ServiceNow applications. Your next step should be to identify scenarios in your current or upcoming ServiceNow projects where leveraging these scratchpad objects can simplify your logic and improve performance.