logo

NJP

" Use of Server side script( g_scratchpad) and client side script "

Import · Dec 19, 2018 · article

Hi Folks,

Definition of g_scratchpad :: The g_scratchpad is an empty object we can use to store information (properties, objects, functions, etc.) from the server side script to the client. It is only available in display

business rules and client scripts.

Suppose we are using server side script and call that script by glide Ajax in client side.For examplethere are times when i need some server side information on the client side and for some reasonwe are calling server side script from an Ajax call .By using the g_scratchpad object, I'll show you how to call server side script functions to "set up" information that the client script can consume.

image

Here is an example based on a customer requirement, present an alert message when an emergency change

request is in the state "Somthing", and has no attachment.

Wouldn't it be nice if there was a client side function to detect if there are attachments on a record?I thought, there's an easy way to do this. From the server side, we can use

current.hasAttachments()

g_scratchpad object is returning the Boolean value True/False(true=this record has attachments, false= this

record has no attachments found.). Using the g_scratchpad object, we can store that value and pick it up with a client script. This is a great place to use a display business rule to do the set up.Lets we are using the g_scratchpad in the code::

Step 1. write business rule :

Name: Check_hasAttachmentsTable: Change Request [change_request]When to run: displayScript:// Check for attachments and save it in the g_scratchpad variable //this is user define variable get_HasAttachments

g_scratchpad.get_HasAttachments = current.hasAttachments();

Step 2. write client script in onLoad Condition :

Name:check_HasAttachment_by_AlertType: onLoadTable: Change Request [change_request]Script:

function onLoad() {

if (g_form.getValue('type') == 'Emergency' && !g_scratchpad.hasAttachments) {

alert("Please attache you attachments");

}

}

You can go to Docs link also: GlideForm ScratchPad

Here you'll find uses for the g_scratchpad and establish the communication more effectivelybetween your server side script and client scripts.

I hope it is helping a lot for beginners and others also .

image

View original source

https://www.servicenow.com/community/itsm-articles/quot-use-of-server-side-script-g-scratchpad-and-client-side/ta-p/2302411