Understanding Scratchpad Part 1 (g_scratchpad)

In ServiceNow, there’s two places where you can use a scratchpad to store data. The first is on form loads and the other is in the workflow. Each works differently and used for different purposes. In general however, they are both use to share information between different areas of the platform.

Today I’ll focus on the form loading which utilises onDisplay business rules as well as the g_scratchpad object. Just to get it out of the way, I think this is one of the most underutilised features on ServiceNow and I’m hoping posting an article about it will get it used (correctly) more often.

So what is it? Well essentially it is a way to pass data from the server to the client without using GlideAjax. The data that you pass through is delivered as part of the rendered page. This is a form of server rendering technology. The data is being shared between the server and the client.

It’s extremely useful for delivering data that there’s a high chance you’ll need (or definitely need) but isn’t there by default. For example, you might want to know the user’s manager, their location, the number of tickets assigned to them etc.

The other way of retrieving this data would be after the form is loaded to perform a GlideAjax back to the server asking for those details making the user wait for them before seeing the results and letting them move on with the process – I’ve seen this too many times and it’s not a nice experience.

Setting up the onDisplay business rule is very simple. Set the ‘when’ value to onDisplay and then set up the g_scratchpad with as many keys as you want. In the below example I create four, myCustomObject, websiteUrl, myManager and myManagerName;

(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', gs.getUserID());
gr.setLimit(1);
gr.query();
if (gr.next()) {
g_scratchpad.myManager = gr.manager + '';
g_scratchpad.myManagerName = gr.manager.getDisplayValue();
}
g_scratchpad.websiteUrl = 'http://servicenowgems.com';
g_scratchpad.myCustomObject = {};
g_scratchpad.myCustomObject.aKey = 'someData';
})(current, previous);

What you’ll notice is I’ve also created an object within the g_scratchpad called myCustomObject. This becomes really useful if you’re working on a complex project with multiple development teams and want to encapsulate you’re data without risk of naming conflicts.

So that’s all there is to the server side. Now how is this delivered to the client? Well if you open up the developer tools and inspect the DOM, and if using the above example, search for servicenowgems and you’ll find a very simple var g_scratchpad = … statement in there with the whole object being created there in a global variable. With a bit of formatting, it looks like:

var g_scratchpad = {
"myManager" : "12345",
"myManagerName" : "Mrs Manager",
"websiteUrl" = "http://servicenowgems.com",
"myCustomObject" : {
"aKey" : "someData"
}
}

So if you want the website url in one of your client scripts, accessing it is super easy and you can run: alert('The website is: ' + g_scratchpad.websiteUrl);

As you can see however, this functionality is all about server side rendering and passing data to the client. If you set g_scratchpad value on the client, it does NOT go back to the server in any way. It’s a one directional process, server to client.

And that’s all there is to how it actually works. The benefits are performance and user experience. No point keeping the user waiting for data you could’ve served up all that time. Important to note though, as you can see here, the variable is delivered on the DOM and so just like any other client script, do not use this for any security related functions! Only use it for UI and guiding the user through the process.

Tomorrow I’ll write up about the workflow scratchpad which is for a completely different use case.

3 Comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s