Client script to return user session time left

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2016 08:20 AM
Is there any javascript that I can put into a Client Script to get the logged in user's session time?
I'm currently doing this in some UI Pages:
var _oneHourTimer = 0;
var tickerSeconds = 3600;
startOneHourTimer();
function oneHourTimerTicker()
{
var el = document.getElementById('sessionReminder');
if (el)
el.innerHTML = ("Your session will expire in " + (--tickerSeconds) + " seconds.");
if (tickerSeconds == 600) // 10 minute warning
alert("Your ServiceNow session will expire in 10 minutes. Be sure to SUBMIT any unsaved form data.");
if (tickerSeconds == 300) // 5 minute warning
alert("Your ServiceNow session will expire in 5 minutes. Be sure to SUBMIT any unsaved form data.");
if (tickerSeconds == 60) // 1 minute warning
alert("Your ServiceNow session will expire in 1 minute. Be sure to SUBMIT any unsaved form data.");
}
function startOneHourTimer()
{
var el = document.getElementById('sessionReminder');
if (el)
el.innerHTML = ("Your session will expire in 1 hour (" + tickerSeconds + " seconds)");
_oneHourTimer = setInterval(function(){ oneHourTimerTicker(); }, 1000);
}
This works fine, but instead of a hard-set guestimation of 3600, I'd like to pull the actual session time left before ServiceNow logs them out.
Any way to pull that data?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2016 09:19 AM
The system property "glide.ui.session_timeout" has the session time out in minutes so that would at least give you a better idea of how long.
Another option is to make a rest call to v_user_session looking for window.NOW.session_id on the client to see what that says. Only issue there is I'm not sure if that will be considered a transaction that keeps the session alive or not. Last I knew a REST call did not keep your session alive but I'm unsure.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2016 10:29 AM
Is there a complete (robust) list of all the methods that gs.getSession() has?
Here, GlideSession - ServiceNow Wiki , they only list a few but looking here gs.getSession().getStack().get(0) indicates there may be many more.
I'm specifically looking for something I can write in javascript (serverside script include) like:
var nowdt = gs.nowDateTime();
var timeLeft = nowdt - gs.getSession().loggedInAt();
(loggedInAt()) is a made up function that I wish were real

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2016 05:33 AM
You can do something like the below to get all of the methods and properties by running as a background script.
var ss = gs.getSession();
for(var x in ss){
gs.print(x);
}
The code you presented is going to return total session time not time left. Time left will be based on the last transaction to the instance since the time out restarts once a transaction is received. This is why I was suggesting the v_user_session table because it shows the last accessed time. If a REST call does not reset it/count as a transaction you can just query that. Again I'm not sure about the REST call, that is something you will have to experiment with.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2019 04:28 AM
Hello all,
I have the session id and user id of the user. I want to create a scripted API which will take input as session id and user and it will check it in logged in user table if the session id or user exist in return it should give me the complete details of the user from user table.
I am struggling with the script, Request you all to help me out on this it is a critical requirement.
Thanks in advance