how does the client scripts differentiate g_scratchpad variables

shefali29
Tera Contributor

Hi

 

how does the client script differentiates which g_scratchpad variable it's utilizing if there exist more than 1 g_scratchpad variable with the same variable name and defined on the same table.

 

TIA

2 REPLIES 2

Community Alums
Not applicable

Hi @shefali29 ,

Why would you use g_scratchpad in client scripts in the first place? why not using  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.

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:

 

g_scratchpad.css = gs.getProperty('css.base.color');
g_scratchpad.hasAttachments = current.hasAttachments();
g_scratchpad.managerName = current.caller_id.manager.getDisplayValue();

 

 

DpkSharma
Giga Expert

Hi @shefali29 

When there are multiple g_scratchpad variables with the same variable name defined on the same table, the client script in ServiceNow differentiates between them based on the scope in which they are defined.

In ServiceNow, the g_scratchpad variable is a temporary variable used to pass server-side values to the client-side when the client needs server data. It is typically initialized and made available to the client script through a display business rule.

The display business rule is responsible for initializing the g_scratchpad variable and making it available to the client script. It sets the values of the g_scratchpad variable based on the server-side data and sends it to the client when the form is requested. This makes the g_scratchpad variable available to all client-side scripting methods.

Therefore, if there are multiple g_scratchpad variables with the same variable name defined on the same table, each g_scratchpad variable will have its own scope within the client script. The client script can access the specific g_scratchpad variable based on its scope and utilize the data stored in that particular variable.

Reference:

 

If it is helpful, please mark it as helpful and accept the correct solution.

Thanks & Regards,

Deepak Sharma