- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-07-2022 01:45 AM
Good morning,
I have a script include with a method in it which should return true/false if the user is logged in or not:
isUserLoggedIn: function(user){
return gs.getUser().getUserByID(user).isLoggedIn();
},
The function is returning undefined.
- The user sys_id is getting passed to the function correctly
- The getUserByID(user) seems to be getting a user 'com.glide.sys.User@*******'
- Script include is in Global, being called from Scoped app
Any ideas why this is returning undefined instead of true/false?
My aim is to check a specific user is logged in (why I have to getUserByID).
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-07-2022 03:47 AM
Hi,
I believe the isLoggedIn() method runs only on getSession()
this table tells you the logged in users of the instance -> sys_user_session
If somebody logs out it removes record of that user from this table
use correct query as per your requirement
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-07-2022 01:59 AM
I do not think that is possible.
Because isInteractive() and isLoggedIn() are all focussed on the current logged in user only.
You will have to gliderecord to the Logged in Users table (v_user_session) to get this job done

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-07-2022 02:03 AM
isUserLoggedIn: function(user){
var isLogged = false;
var loggedUser = new GlideRecord('v_user_session');
loggedUser.addActiveQuery();
loggedUser.addQuery('user' , user);
loggedUser.query();
if(loggedUser.next())
{
isLogged = true;
}
return isLogged;
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-07-2022 02:33 AM
Thanks for your reply, I have done something similar, but the query is not finding the user in the v_user_session table.
- I am logged in as the user on a different browser (not impersonating)
- When I go to the session table as admin, I cannot see their session, presume this is because I am on a different node? Would this be the same reason the query cannot see it either?
isUserLoggedIn: function(user){
var isLogged = false;
var userGr = new GlideRecord('sys_user');
userGr.get(user);
gs.info('crisUn ' + userGr.user_name); //get username to query
var sessionGr = new GlideRecord('v_user_session');
sessionGr.addQuery('user', userGr.user_name); //user field on user session table is string type with user_name
sessionGr.query();
if(sessionGr.hasNext()){
gs.info('crisU is online ' + isLogged);
isLogged = true;
}
gs.info('crisU is offline ' + isLogged);
return isLogged;
//return gs.getUser().getUserByID(user).isLoggedIn();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-07-2022 02:54 AM
Is the user parameter passed into the function a user sysid?
It needs to be a sys id.
I just tested your code and it worked fine for me. I was able to find myself as well as other members who are logged in based on their sysids