Apply effects of User Criteria without the need to log out and back in – ServiceNow

Have you ever had a need to apply the effects of user criteria immediately, after the user performed certain actions, for example, changed his location or his language, therefore he must see different items in the catalog or the knowledge base immediately?

The out of the box way to apply User Criteria is tedious. First, the user needs to log out, which is something that everyone hates. The user has to go through multiple SSO white flashing screens or he has to re-enter his password. Overall, it might take up to 15 seconds untill user can use ServiceNow again.

Secondly, users want the effect to happen immediately. If you have changed your location from Norway to Sweden and you are still seeing only location specific requests for Norway employees, you might think it’s not working as expected. Everyone hates situations when they call Service Desk complaining something is not working and they are presented the same old question – ‘Have you tried logging out and in?’. There are no winners here.

Log out requirement is done for performance reasons: User Criteria is cached and is only build once, when the user has logged in.

There are some drawbacks in OOTB UC set up:

1) User needs to log out and back in which can take some time.

2) Complete user cache has to be rebuilt from 0. I would think this has quite some performance impact compared what I will suggest next.

Let’s say the criteria is based on user’s language and the user is using service portal. Let’s make sure he has no need to log out to see his language-based catalog items.

Now let’s create some Scripted user criteria records which would confirm the user criteria was applied immediately. Do this for Sweden and Norway. As in example:

Add the user criteria to some catalog items. Let’s say iPad’s can only be ordered if your language is Swedish, and iPhone’s if it’s Norwegian. Now let’s open the Service Portal:

You can see both criteria’s were evaluated and Sweden one was applied, however, when we change our language to Norway, this is not re-displayed and the criteria is not re-applied until we log out or clear server cache.

So how can we do it without logging out or clearing all server cache?

By clearing user’s private user criteria cache object whenever he changes his language.

If we use cache inspector tool (more on my previous article), we can locate the UC cache object name: criteria_cache.

To add it to the portal let’s modify the ‘User Profile’ Service Portal widget by cloning it and adding the copy to our page. Modify the server code whenever user language is changed to clear his UC cache with this code:

gs.cacheflush(“criteria_cache”);

That’s it! No more need to log out and back in. Effects are immediate once you go back to the catalog.

Does this kind of scripted user criteria have performance implications?

No, because there is another user object cached which holds all user profile specific information, including his language setting. Therefore, user criteria scripts are only looking for the value in the cache and not performing any database queries.

How to call UI actions with REST API or AJAX – ServiceNow

You can execute UI Actions without actually clicking them, but by integrating them in your integrations flow.

Note: you should only follow this guide if you are on any version prior Madrid. If you are using Madrid version or up, there is OOTB way to call UI actions. Click here.

First, let’s prepare our environment. Open google chrome, open any change record, press F12 to access developers tools, hit the Network tab, press the clear button to clear everything and check the box next to the “Preserve Log” button.

Now, lets examine the behavior of normal UI action interaction – in the change record press “Execute Risk calculation” UI action. In the networks tab we can see exactly what happened – HTTP POST request was sent to https://devXXXXX.service-now.com/change_request.do. The user-agent (Google chrome) parsed all of our information from our interaction with the web page and created a HTTP POST request.

If we know how the user-agent sends this HTTP request, can we do the same with vanilla JavaScript and REST API‘s ourselves? Let‘s have a look.

First, let‘s ignore the information we do not need to call our UI action. We only need:

sysparm_ck – our session token

sys_action – sys_id of UI action

sys_base_uri – our instance URL

sys_target – target table

sys_uniqueValue – sys_id of the change record

Let’s first try XMLHttpRequest with vanilla JavaScript straight from our browser console.

// First, we create XMLHttpRequest object

var XHR = new XMLHttpRequest();

// Now we configure our request parameters

var urlEncodedData =

“sys_base_uri=https%3A%2F%2FdevXXXXX.service-now.com%2F” + // This is percent encoded URL for our instance

“&sys_target=change_request” + // Target table the UI action is set for

“&sys_uniqueValue=016e2b3ddb3223009373793ebf9619ff” + // Target change record sys_id

“&sys_action=eea1aa78c0a8ce01002aa530a8f1eb65” +  // Sys_id of the Risk assesment calculation UI action

“&sysparm_ck=c42d5306dbXXXXXXXX73793ebf9619XXXX0aad94eXXXXccc1XXXX801dXXXX6a8af9aaee1”; // Our SNOW session Token

 // Set up our request

 XHR.open(‘POST’, ‘https://devXXXXX.service-now.com/change_request.do’);

 // Add the required HTTP header for form data POST requests

 XHR.setRequestHeader(‘Content-Type’, ‘application/x-www-form-urlencoded’);

 // Finally, send our data.

 XHR.send(urlEncodedData);

Note that any non-alpha numerical characters have to use percent-encoding.

After XHR.send method we can see that the risk assessment calculation was performed and console logged change.updated event.

Now let‘s do the same with REST API using POSTMAN.

But how do we get the session token value first? This means that a session has to exist in current SNOW sessions list, otherwise it will not work. So we have to make 2 REST API calls for this to fully work.

Before we have our full integration running, we need to establish a Session with SNOW, retrieve our session token, then make our call to execute the UI action.

We need to create a scripted REST API that returns our session token by returning gs.getSessionToken() string. Let’s assume that we have the sys_id of our change record already from Table API.

Create POST API with /api/XXXXXX/sessiontoken/{name} resource path.

Since we cannot use gs.getSessionToken() API in our REST script, we have to gliderecord into sys_user_session table to get the csrf token.

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

                var body = {};

                body.name = request.pathParams.name;

                var getToken = new GlideRecord(‘sys_user_session’);

                getToken.addQuery(‘name’, body.name); // We use user name as query param

                getToken.query();

                if (getToken.next()){

                                body.token = getToken.csrf_token;

                }

                response.setBody(body);

})(request, response);

We make the call in POSTMAN to get the token:

And we make the second call using the token to execute UI action.

That‘s it! Now with 2 API calls we can call any SNOW UI action from anywhere!