Sanjay Bagri1
Tera Guru

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 example
there are times when i need some server side information on the client side and for some reason
we 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.

find_real_file.png
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_hasAttachments
Table: Change Request [change_request]
When to run: display

Script:
// 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_Alert
Type: onLoad
Table: 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 effectively
between your server side script and client scripts.
   
I hope it is helping a lot for beginners and others also .

Comments
masnsa
Tera Contributor

does g_scratchpad also works on OnChange and onSubmit Client scripts

 

Sanjay Bagri1
Tera Guru

Yes it will work 

HussainBasB
Tera Explorer

Hi Sanjay,

   As you told, g_scracthpad will aslo work in 'On submit CS' . I am using below mentioned script but it is not working. Can you help me.

 

Display Business Rule:
var gr = new GlideRecord('sys_user');
    gr.addQuery('sys_id', current.u_caller);
    gr.query();
    var isVIP = false;
    if (gr.next()) {
        if (gr.vip) {
            isVIP = true;
        }
    }

    g_scratchpad.cyd = isVIP;
 
On Submit CS:
if (g_scratchpad.cyd == false) {

        g_form.addErrorMessage('VIP caller only can raise critical');
        return false;
    }
VanshAgarwal
Tera Contributor

Hello @HussainBasB 

On which table are you creating this Business Rule.

BillMartin
Mega Sage

I created this full live demo on how to use g_scratchpad object. hope this helps 🙂

 

Daniel Oderbolz
Kilo Sage

BTW: g_scratchpad is also a great way to add scripts that need to be called from several other scripts.

So in an onLoad Script you can put something like

g_scratchpad.checkFunction = function()
{
// Perform checks
}

And then you can use this in all Client Scripts of the given Catalog Item or Variable set.

Version history
Last update:
‎12-18-2018 11:23 PM
Updated by: