Display Business Rule Demo | g_scratchpad Variable Demo | ServiceNow Scripting 6
Table of Contents
Introduction
In this tutorial, we will explore how to use display business rules and the g_scratchpad variable in ServiceNow. Display business rules are key for executing logic just before a form is presented to the user, allowing you to manipulate data as needed. We'll also provide a practical example of how to use the g_scratchpad variable to transfer data from the server side to the client side.
Step 1: Understanding Display Business Rules
- Display business rules execute before the form is shown to the user, immediately after the data is retrieved from the database.
- This brief window allows you to make changes or pass information that is not directly visible on the form.
- Use this functionality to prepare data that needs to be sent to the client side but isn't displayed on the form.
Step 2: Creating a Display Business Rule
- Navigate to the Business Rules module in ServiceNow.
- Create a new business rule and select the following options:
- Set the "When" field to "Display".
- Choose the appropriate table you want to target (e.g., Incident).
- In the script section, use the g_scratchpad variable to store values from the record. For example:
g_scratchpad.businessService = current.business_service.getDisplayValue();
- This line stores the business service value from the current record into the g_scratchpad variable.
Step 3: Utilizing g_scratchpad in Client Scripts
- Create a client script that runs on form submission.
- Use the g_scratchpad variable to retrieve the value stored earlier. Here’s how to do it:
alert('Value from g_scratchpad: ' + g_scratchpad.businessService);
- This will display the business service in an alert box when the form is submitted.
Step 4: Testing the Implementation
- Open an incident that has a business service value set (e.g., "SA Financial Accounting").
- Change any field in the incident to trigger the onSubmit client script.
- When you submit the form:
- The first alert (using g_form.getValue) may return null if the field is not displayed on the form.
- The second alert (using g_scratchpad) should correctly display "SA Financial Accounting".
Conclusion
In this tutorial, we've covered how to create display business rules in ServiceNow and how to effectively use the g_scratchpad variable to transfer data from the server to the client side. This method is particularly useful for accessing values that are not shown on the form. For further learning, explore additional ServiceNow scripting tutorials or dive into more advanced scripting techniques to enhance your skills.