The CreatorCon Call for Content is officially open! Get started here.

Share a javascript variable between client scripts on the same form.

BrisRob
Kilo Explorer

I have a two client onchange scripts that both need to adjust a javascript variable. Both scripts are started by a change in different fields on the form. one script adds 1 to the variable, the other subtracts one from the variable. Is there anyway for these two scripts to share access to the same  javascript variable?

3 REPLIES 3

Raju Koyagura
Tera Guru

Did you try with g_scratchpad

 

https://servicenowgems.com/2016/10/10/understanding-scratchpad-g_scratchpad/

This will use a call to the server correct? In the native UI I can declare:
var count = 27;

and in two different onchange client scripts i can use or change the variable count and i do not need to contact the server.
However in the serviceportal I can not declare a variable and then access it in a different client script of the same form.

I'm curious if "g_scratchpad" Object is the proper way to share data across client scripts.

The post you have mentioned describes the way to pass data from a Business Rule to Client Script. The same use case we can find in official docs.

The "g_scratchpad" look really like it would be a good and safe place for sharing and persisting data. I would really like to hear something from any ServiceNow Platform Developer about this topic.