This article is based on the ServiceNow documentation article. See the original article on the ServiceNow doc site: Minimize Server Lookups.
Client scripting uses either data available on the client or data retrieved from the server. Use client data as much as possible to eliminate the need for time-consuming server lookups. The top ways to get information from the server are g_scratchpad, and asynchronous GlideAjax lookup.
The primary difference between these methods is that g_scratchpad is sent once when a form is loaded (information is pushed from the server to the client), whereas GlideAjax is dynamically triggered when the client requests information from the server.
Other methods, GlideRecord and g_form.getReference() callback are also available for retrieving server information. However, these methods are no longer recommended due to their performance impact. Both methods retrieve all fields in the requested GlideRecord when most cases only require one field. The GlideRecord API is not available for scoped applications.
The g_scratchpad object passes information from the server to the client, such as when the client requires information not available on the form. For example, if you have a Client Script which needs to access the field u_retrieve, and the field is not on the form, the data is not available to the Client Script. A typical solution to this situation is to place the field on the form and then always hide it with a Client Script or UI Policy. While this solution may be faster to configure, it is slower to execute.
If you know what information the client needs from the server before the form is loaded, a display Business Rule can create g_scratchpad properties to hold this information. The g_scratchpad object is sent to the client when the form is requested, making it available to all client-side scripting methods. This is a very efficient means of sending information from the server to the client. However, you can only load data this way when the form is loaded. The Business Rule cannot be triggered dynamically. In those cases, use an asynchronous GlideAjax call.
For example, assume you open an incident and need to pass this information to the client:
- The value of the system property css.base.color
- Whether or not the current record has attachments
- The name of the caller’s manager
A display Business Rule sends this information to the client using the following script:
Example: Asynchronous GlideAjax
This script compares the support group of the CI and the assignment group of the incident by name:
This script relies on an accompanying script include, such as:
Use setValue()’s displayValue Parameter with Reference Fields
When using setValue() on a reference field, be sure to include the display value with the value (sys_id). If you set the value without the display value, ServiceNow does a synchronous Ajax call to retrieve the display value for the record you specified. This extra round trip to the server can leave you at risk of performance issues.