What is scratchpad ?

Sooraj Singh1
Tera Contributor

What is scratchpad in BR or in general g_scratchpad

1 ACCEPTED SOLUTION

Alikutty A
Tera Sage

Hello,

Scratchpads are temparory client side variables that can be used in BR and client scripts. They avoid the need of making additional server side calls from client scripts by pre-fetching server data initially when a form is loaded. When you open a form, it will need to display form data and will query this data from the tables. At this point you can define display business rules which can save any server side data within g_scratchpad variables. These variables are accessible within all client script types and allows you to avoid making any additional server side calls. 

Eg: On incident form, you need to add a client side validation on VIP users. ie add a pop up alert if caller is VIP. On the client side, you do not know whether the current caller is a VIP or not and you are required to fetch this data from server side by querying on the user table and then fetching the VIP attribute. This would be either through a GlideRecord or GlideAjax or getReference calls. You could avoid making these additional server side calls by wrting a display business rule which can fetch this value when the form is opened or displayed to user

A display BR with the below script can be written

g_scratchpad.isVIP = current.caller_id.vip;

Now you could access this variables within any of your client scripts and use it for validation

eg, an onLoad client script can use it as

if(g_scratchpad.isVIP == true){

  alert("Caller is a VIP user");

Thanks!

View solution in original post

5 REPLIES 5

Alikutty A
Tera Sage

Hello,

Scratchpads are temparory client side variables that can be used in BR and client scripts. They avoid the need of making additional server side calls from client scripts by pre-fetching server data initially when a form is loaded. When you open a form, it will need to display form data and will query this data from the tables. At this point you can define display business rules which can save any server side data within g_scratchpad variables. These variables are accessible within all client script types and allows you to avoid making any additional server side calls. 

Eg: On incident form, you need to add a client side validation on VIP users. ie add a pop up alert if caller is VIP. On the client side, you do not know whether the current caller is a VIP or not and you are required to fetch this data from server side by querying on the user table and then fetching the VIP attribute. This would be either through a GlideRecord or GlideAjax or getReference calls. You could avoid making these additional server side calls by wrting a display business rule which can fetch this value when the form is opened or displayed to user

A display BR with the below script can be written

g_scratchpad.isVIP = current.caller_id.vip;

Now you could access this variables within any of your client scripts and use it for validation

eg, an onLoad client script can use it as

if(g_scratchpad.isVIP == true){

  alert("Caller is a VIP user");

Thanks!

Rahul Kumar17
Tera Guru

Hi,

Go for this link

https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/app-store/good_practices/client_s...

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar

@Rahul Kumar It seems that link is no longer functional...  Maybe this will help:

   GlideFormScratchpad - Scoped, Client

 

Server Side Business Rule...
Advanced = true, When = display, Script =

(function executeRule(current, previous /*null when async*/) {
	g_scratchpad.MyOwnVariable = "XYZ";
})(current, previous);

 

Client Side Script...
Script =

function onLoad() {
	g_form.setValue("description", g_scratchpad.MyOwnVariable);
}

Geoffrey_Bishop
Giga Expert

@Rahul Kumar It seems that link is no longer functional...  Maybe this will help:

   GlideFormScratchpad - Scoped, Client

 

Server Side Business Rule...
Advanced = true, When = display, Script =

(function executeRule(current, previous /*null when async*/) {
	g_scratchpad.MyOwnVariable = "XYZ";
})(current, previous);

 

Client Side Script...
Script =

function onLoad() {
	g_form.setValue("description", g_scratchpad.MyOwnVariable);
}