- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2018 06:54 AM
Hello Everyone,
Got a question regarding all currently logged users. I need to get the list of all logged users that are assigned to a group. Below is the script I got so far to get all the users that are currently in the assignment group and save it in an array
var groupName = current.assignment_group;
var members = [];
var gp = new GlideRecord('sys_user_grmember');
gp.addQuery('user.active', true);
gp.addQuery('group', groupName);
gp.query();
while (gp.next()) {
if(gp.getUser())
{
members.push(String(gp.user));
}
}
I need to save the user to the array only if the user has logged in within last five minutes. Please advice
Thanks,
Tom
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2018 07:38 AM
OK this script should work for you:
var minutesAgo = 5;
var groupName = current.assignment_group;
var members = [];
var gp = new GlideRecord('sys_user_grmember');
gp.addQuery('user.last_login_time', '>=', gs.minutesAgo(minutesAgo));
gp.addQuery('user.active', true);
gp.addQuery('group', groupName);
gp.query();
while (gp.next()) {
var gpMember = gp.user.toString();
if (members.toString().indexOf(gpMember) == -1) {
members.push(gpMember);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2018 07:00 AM
Toms can you please define "logged in user"? Is that users that are actively in the system when you run this query? Or is it a list of users that have logged in within X timeframe?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2018 07:06 AM
when I ran the query, I need to get all the list of users that have logged in within last 5 minutes. sorry about the confusion
Thanks,
Tom
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2018 07:38 AM
OK this script should work for you:
var minutesAgo = 5;
var groupName = current.assignment_group;
var members = [];
var gp = new GlideRecord('sys_user_grmember');
gp.addQuery('user.last_login_time', '>=', gs.minutesAgo(minutesAgo));
gp.addQuery('user.active', true);
gp.addQuery('group', groupName);
gp.query();
while (gp.next()) {
var gpMember = gp.user.toString();
if (members.toString().indexOf(gpMember) == -1) {
members.push(gpMember);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2018 01:17 PM
I would avoid dot walking in a query like that. Doing that means another another server call to query and get that value. In this case, it means that for each record in the sys_user_grmember it needs to do 3 server calls. 1 to get the record, 1 to get the user.last_login_time value and 1 to get the user.active value.