gs.getUser().getUserByID(user).isLoggedIn(); not working in script include

Cris P
Tera Guru

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).

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

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

find_real_file.png

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

10 REPLIES 10

AnirudhKumar
Mega Sage
Mega Sage

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

AnirudhKumar
Mega Sage
Mega Sage
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;


	},

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();
	}

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