Determine if a user by user_name is logged into ServiceNow (has an active, legit session)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2022 11:58 AM
I have a script include that I call which has a function to determine if a user (by user_name) is logged into ServiceNow (has an active session).
I've read posts all over the community but am still confused by what is the actual correct answer, hence the reason I'm combining what I've read into this:
// Beginning attempt to figure out if a user (by user_name) is logged into ServiceNow (has an active session)
var vSession = new GlideRecord('v_user_session');
vSession.addQuery("user", user);
vSession.query();
if (vSession.next()) {
var uSession = new GlideRecord('sys_user_session');
uSession.addEncodedQuery("nameISNOTEMPTY^invalidatedISEMPTY^last_accessedONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^name="+user);
uSession.query();
if (uSession.next())
return 'yes';
}
// Ending attempt.
The full Script Include is pasted below for context.
var roundRobin_functions = Class.create();
roundRobin_functions.prototype = Object.extendsObject(AbstractAjaxProcessor, {
"getUserNameBySysId" : function(sysId){
var gr = new GlideRecord('sys_user');
if (gr.get(sysId)){
if (gr.user_name != '')
return gr.user_name+"";
}
return false;
},
"isLoggedIn" : function(user, autoLogout){
if (!user)
userName = this.getParameter('sysparm_user');
if (!user)
return 'missing user_name andOr sys_id';
if (user && user.toString().length == 32){
user = this.getUserNameBySysId(user);
if (!user)
return 'could not get a user_name by: '+user+"";
}
// Beginning attempt to figure out if a user (by user_name) is logged into ServiceNow (has an active session)
var vSession = new GlideRecord('v_user_session');
vSession.addQuery("user", user);
vSession.query();
if (vSession.next()) {
var uSession = new GlideRecord('sys_user_session');
uSession.addEncodedQuery("nameISNOTEMPTY^invalidatedISEMPTY^last_accessedONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^name="+user);
uSession.query();
if (uSession.next())
return 'yes';
}
// Ending attempt.
if (autoLogout){
var rr = new GlideRecord('u_round_robin');
rr.addEncodedQuery("u_active=true^u_type=User Options^u_is_logged_into_rr=true^u_user.user_name="+user);
rr.query();
if (rr.next()){
rr.u_is_logged_into_rr = false;
rr.update();
}
}
return 'no';
}
});
Will the code above be 100% accurate in telling me if a sys_user record has an active/valid/live ServiceNow session? Really meaning, is user X logged in or not? I'm amazed at how difficult of a question this seems to be to answer and/or why there's not a single universal answer. I'm sure this is a very common question.
Yes, I know about "gs.isLoggedIn()"... but I want to pass a user_name to find out if they're logged in or not. Does the GlideSession "isLoggedIn()" accept parameters? Ex: gs.isLoggedIn('xiaix');
It doesn't look like it: Docs GlideSession - isLoggedIn - Global
Thanks for looking!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2022 12:20 PM
Hey,
isLoggedIn can't be used for a particular user. Only works for currently logged in user.
Follow below article:
How to identify all the currently logged in Users and having a valid session ID?
Feel free to mark correct, If I answered your query.
Will be helpful for future visitors looking for similar questions 🙂
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2022 12:44 PM
Yes, I came across that article as well before I posted this question. That's where I got the idea to query the sys_user_session table, but then I went one step deeper to query the v_user_session table (just in case?).
Thanks for confirming that the GlideSession "isLoggedIn()" method cannot accept parameters. That's what I was disappointed to assume 😞
Again, I'm asking if this makes sense and will give me a 100% accurate result:
// Beginning attempt to figure out if a user (by user_name) is logged into ServiceNow (has an active session)
var vSession = new GlideRecord('v_user_session');
vSession.addQuery("user", user);
vSession.query();
if (vSession.next()) {
var uSession = new GlideRecord('sys_user_session');
uSession.addEncodedQuery("nameISNOTEMPTY^invalidatedISEMPTY^last_accessedONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^name="+user);
uSession.query();
if (uSession.next())
return 'yes';
}
// Ending attempt.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2022 12:58 PM
This is not a OOB so can't really give seal of approval here but can be tested in the instance, with different use cases to confirm.
You can check for set of diff users, users with roles, no roles, admins, ones who logged in using SSO and also those with local credentials. A recently deactivated user, that should be enough to conclude.
Feel free to mark correct, If I answered your query.
Will be helpful for future visitors looking for similar questions 🙂
Aman Kumar