g_scratchpad vs GlideDialogWindow.setPreference

Nacho Salamanca
Tera Contributor

Hi,

 

I am rendering a modal (an UI Page) from an UI Action in a form and I need to send some values in order to do some operations from the modal's client script.

To do this, I have found 2 different ways: save the value through g_scratchpad or use the function "setPreference". Both are working properly but I would like to know which is the best option and the differences between two.

 

Could someone enlighten me with their wisdom? 🙂

 

Thanks in advance.

1 ACCEPTED SOLUTION

Anand Kumar P
Giga Patron
Giga Patron

Hi @Nacho Salamanca ,

 

g_scratchpad is like a sticky note:

  • Use it when you need to pass a quick message or reminder within the same session.
  • It's temporary, like a sticky note that you use and throw away.
  • Best for small, short-lived messages or data.

setPreference is like a diary:

  • Use it for storing personal notes or preferences that you want to keep for a long time.
  • It's more permanent and tied to your profile, like a diary you can revisit.
  • Not the best choice for quick, temporary messages between a UI action and a UI page.

So, for passing data between a UI action and a UI page within the same session, the best choice is g_scratchpad. It's quick and disposable, perfect for short-term communication. If you need to remember something for a long time or store personal preferences, then setPreference is more suitable, but it's not ideal for quick communication between a UI action and a UI page.
Thanks,

Anand

View solution in original post

4 REPLIES 4

Allen Andreas
Administrator
Administrator

Hi,

The setPreference method is the one most commonly suggested and would be what I'd use. The g_scratchpad isn't necessary in this case as it would always be retrieving a value whereas setPreference is only executed if the UI Action is utilized.


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

The g_scratchpad values are being set in the UI Action, therefore both ways are only executed if the UI ACtion is utilized.

Anand Kumar P
Giga Patron
Giga Patron

Hi @Nacho Salamanca ,

 

g_scratchpad is like a sticky note:

  • Use it when you need to pass a quick message or reminder within the same session.
  • It's temporary, like a sticky note that you use and throw away.
  • Best for small, short-lived messages or data.

setPreference is like a diary:

  • Use it for storing personal notes or preferences that you want to keep for a long time.
  • It's more permanent and tied to your profile, like a diary you can revisit.
  • Not the best choice for quick, temporary messages between a UI action and a UI page.

So, for passing data between a UI action and a UI page within the same session, the best choice is g_scratchpad. It's quick and disposable, perfect for short-term communication. If you need to remember something for a long time or store personal preferences, then setPreference is more suitable, but it's not ideal for quick communication between a UI action and a UI page.
Thanks,

Anand

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Nacho Salamanca ,

 

1. **`g_scratchpad`:**
- `g_scratchpad` is a client-side JavaScript object in ServiceNow that allows you to store data temporarily on the client (browser) side.
- It can be used to store data during the processing of a client-side script and then access that data in subsequent client-side scripts.
- It is specific to the current user's session and can be used to maintain data across different client-side scripts.

2. **`GlideDialogWindow.setPreference`:**
- `GlideDialogWindow` is a JavaScript class used to create and manage modal dialog windows in ServiceNow.
- `setPreference` is a method of the `GlideDialogWindow` class, used to set preferences for a GlideDialogWindow before it is displayed.
- Preferences set using `setPreference` are passed from the server-side script to the client-side dialog window.
- This method is useful when you need to pass specific data from server-side to a modal dialog window.

**Difference:**
- `g_scratchpad` is used to store temporary data on the client-side, specific to the current user's session, and is accessible across various client-side scripts.
- `GlideDialogWindow.setPreference` is used to pass data from the server-side script to a specific instance of a modal dialog window.

Choosing between them depends on your use case:
- If you need to store temporary data on the client-side and access it across different client-side scripts, use `g_scratchpad`.
- If you are working with a modal dialog window and need to pass data from the server-side script to the client-side dialog window, use `GlideDialogWindow.setPreference`.

 

Please mark my answer helpful & accepted if it helps you resolve your query.

 

Thanks,

Danish