Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Killing user session

Priyanka Palask
Tera Contributor

I want to kill user session which are active since last 60 minutes. 

Referring to below code snippet from various post and ServiceNow also sent this to me: 

 

var exclusionList=['integration_user','svc_account'];
var gr=new GlideRecord('sys_user_session');
gr.addQuery("sys_created_on<javascript&colon;gs.dateGenerate('"+sma+"')");
gr.addQuery('user.user_name','NOT IN', exclusionList);
gr.query();
 
But how can I check which user session is that, there is no "user" field on sys_user_session table. How we can apply second addQuery above to sort the session with user exclusions.
 
Appreciate your quick help
1 ACCEPTED SOLUTION

J Siva
Kilo Patron

Hi @Priyanka Palask 
You can use the 'name' field.

JSiva_0-1745296855023.png

 

var exclusionList=['integration_user','svc_account'];
var gr=new GlideRecord('sys_user_session');
gr.addQuery("sys_created_on<javascript&colon;gs.dateGenerate('"+sma+"')");
gr.addQuery('name','NOT IN', exclusionList);
gr.query();

Regards,
Siva

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron

@Priyanka Palask 

the name field on table "sys_user_session" holds username

Also you can't delete the record from this table as deleting doesn't help here.

you need to set locked field on table "v_user_session" to true so kill session

So please enhance the script for that table "v_user_session"

Something like this but please enhance

var gr = new GlideRecord('v_user_session');
// give proper query if you want using addQuery or addEncodedQuery
if (gr.get('user', 'username_of_user_to_kill')) { // Replace username_of_user_to_kill with the user's username
  gr.locked = true;
  gr.update();
}

AnkurBawiskar_0-1745297414782.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Priyanka Palask 

Did you get a chance to check my above response?

Deleting records from "sys_user_session" won't help.

If my response helped please mark it correct as well so that it benefits future readers.

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