- 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 01:38 PM
Fair enough. The following should provide the same results
var minutesAgo = 5;
var groupName = current.assignment_group;
var members = [];
var gp = new GlideRecord('sys_user_grmember');
gp.addQuery('group', groupName);
gp.query();
while (gp.next()) {
var gpMember = gp.user.toString();
if (members.toString().indexOf(gpMember) == -1) {
// Validate last login time
var userRec = new GlideRecord('sys_user');
userRec.addQuery('sys_id', gpMember);
userRec.addQuery('last_login_time', '>=', gs.minutesAgo(minutesAgo));
userRec.addQuery('active', true);
userRec.query();
if (userRec.hasNext()) {
members.push(gpMember);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2018 01:49 PM
I would query the user table first -> put members in array, and then query the group membership with query if the user exist within the array. Then loop through the grmember records to push the sys_ids of user.
By doing this you avoid nestled queries like about. In that case you will have 1 server call for the grmember and then x amount of server call for each grmember record. Example above is 2 server call... And I know, I'm a performance junkie (Sorry)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2018 07:27 AM
Hi Toms,
Here is a nice blogpost how to get all the current logged in users that you can compare with: https://servicenowgems.com/2016/04/28/finding-logged-in-users/
I would rather go with a script something like this:
1. Take a loggedin users and put their sys_id in a array, make a copy of the array to keep the original
2. make a query to sys_user_grmember with only fetching record having a user id that exist in your array (gr.addQuery('user','IN',NAME_ARRAY);
3. Then go through those records and see if the user exists in the record, put that sys_id in a new array. this can be done with filter(). Then at the end you will have a list of users that are member of a group. Probably want to run it through unique as well otherwise you will have duplicate sys_ids if the user is in more than one group.
Might be able to trim it later on. All depends how ofter you going to run this.
Let me know if you need more help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2018 07:31 AM
missed your post about it was those who where last loggedin 5 min.. then you can query the user table, since there is a last login field (last_login_time) that updates when a user logged in. Remember thou that the user might logged out as well.