g_scratchpad in servicenow

What is g_scratchpad?

g_scratchpad is an OOB object, present on client and server side. It is used for passing information from server to client without using glideAjax() call. If there is a high chance that you will need data from server to client but isn’t available by default. On that case It is the best practice to use g_scratchpad variable.

For example, you might want to know the user’s manager, their location, the number of tickets assigned to them etc.

g_scratchpad in servicenow - servicenowwithrunjay.com

We already know that GlideAjax can be used to retrieve data from server but still we are talking about g_scratchpad variable why? Let’s understand this with an example.

Suppose we want to fetch user company name from server.

If we go ahead with GlideAjax, it means it will get invoked after the form load and user has to wait for server response without seeing the result. On second had if we are going ahead with g_scratchpad, it will bring data before form loading.

Note: GlideAjax will have more impact as compare to g_scratchpad. I have seen this too many cases and it’s not a good experience to use GlideAjax over g_scratchpad.

 

 

How to use g_scratchpad?

Step 1: Create display business rule and set the value in g_scratchpad.

Step 2: write any client-side script get the g_scratchpad value.

Now lets try to understand with an real time scenario.

Real World Scenario

Allow to resolve the incident (which belongs to category software) only if there is an attachment associated with it if not then display message stating “First upload the approved/signed email from “Caller’s manager””.

Solution

  1. Create display business rule on incident table, put filter condition as active is true. In script set caller’s manager name in one variable and attachment status in another.
    g_scratchpad.hasAttachment = current.hasAttachments(); 
    g_scratchpad.callerManager = current.caller_id.manager.getDisplayValue();
    

 

  1. Create On-change client script on state filed. In script check category and state value, if category is software and state is resolved then perform validation. If its fails then display message and re-set the state option back to previous one.
     if (g_form.getValue('category') == 'software' && newValue == 6) { //Resolved- 6   
            var hasAttachment = g_scratchpad.hasAttachment;
            var callerManager = g_scratchpad.callerManager;
    
            if (!hasAttachment) {
                g_form.setValue('state', oldValue);
                alert('First upload the approved or signed email from your manager ' + callerManager);
            }
        }

Output:

 

 

Join Runjay Patel

Thanks for reading this article, i hope you liked it, if that so, do like and subscribe my YouTube channel. You can also join our telegram channel to clear your technical doubt.

LEAVE A REPLY

Please enter your comment!
Please enter your name here