We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

marcguy
ServiceNow Employee

Have you found yourself frequently writing the same client scripts, glideAjax calls and Script Includes to access certain data about the current user? i.e. which groups they belong to, which roles they have in those groups, their country/location/manager etc.

 

Well, if you do access the same data frequently, or find it hard to write particular client side scripts to capture this data, there is a method to actually set and store the client session data, then access it on demand in the client with one line. So I thought I would give a couple of practical examples where this can come in handy and help reduce those glideAjax calls even when they are running server side, asynchronously etc.

 

The first thing you need to decide is which data would be useful to you in your client scripts. Once you know what is useful, you can create a Script Action to run on the event 'session.established' (i.e. when a user logs in).

 

Here are two examples of how you can reduce client side lookups.

 

Capture the groups the user belongs to plus a few other useful items that you can use later

 

Script Action.png

 

So we now have this data being collected on login (note: this data is only collected once, so if a user changes groups or any other data, the data will still be the login data).

 

Now, how do we access this data? Well, it's just one line of code:

 

var myGrps = g_user.getClientData('u_userGroupList');

 

It is a lot quicker than writing glideAjax to return users groups right?

 


Use a client script to check if the current user is a member of the assignment group and prompt them be assigned to an incident

 

Client Script.png

 

I'm sure you can find many more uses for this method but checking for the groups the user belongs to is quite a common activity. Hopefully, this example will help and please let me know if you find other ways of using this and saving yourselves some time.

 

What data are you constantly having to evaluate that could be stored in the session?

17 Comments
ssb
Tera Guru

I'd be interested to know how this impacts memory usage in an instance?



I guess since it only allows string data it would be hard to really jam up the memory much (maybe if someone tried to push XML documents into the session or something).


Chandan23
Tera Expert

good article, and very much useful...


marcguy
ServiceNow Employee

Hi Scott, good question, I haven't got any hard data but as you say what we are storing is normally very light and from experience this hasn't caused any issues where I've used it on previous implementations.


manish_vinayak
Kilo Expert

This seems to be really useful. Thanks for sharing.


Marc A Love
Giga Expert

I was looking for a way to clean things up....this will help a lot.....thanks!


Kalaiarasan Pus
Giga Sage

this is just the thing which I was looking for May thanks



A suggestion though...


wouldn't it be useful to include some tips about display BR and its use to avoid Ajax calls


marcguy
ServiceNow Employee

You've just given me my next blog post, great idea! thanks, Marc


Not applicable

This is fantastic, thank you!


Ashok10
Mega Expert

good one


vongernation
Giga Contributor

Is there any way to trigger this with a custom event?   We want to do the putClientData sometime after login, but it doesn't appear to work for any event other than session.established.