- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2023 12:08 PM
Hello Team
i need to know that how this line is working in code (inactiveUsers.hasNext())
//Fetch for inactive users.
var inactiveUsers = new GlideRecord('sys_user');
inactiveUsers.addQuery("active", "false");
inactiveUsers.addQuery('sys_id', 'IN', users);
inactiveUsers.query();
//Check for any inactiveusers.
if (inactiveUsers.hasNext()) {
var userArray = users.split(",");
while (inactiveUsers.next()) {
var useridx = userArray.indexOf(inactiveUsers.getUniqueValue()+'');
if (useridx != -1)
userArray.splice(useridx, 1);
}
users = userArray.toString();
}
return users;
because when i am running below code in background script which is getting the record
var inactiveUsers = new GlideRecord('sys_user');
inactiveUsers.addQuery("active", "false");
inactiveUsers.addQuery('sys_id=62826bf03710200044e0bfc8bcbe5df1');// This is active user id
inactiveUsers.query();
if(inactiveUsers.hasNext())
{
gs.info("user is active");
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2023 12:11 PM
Hello,
My apologies, your post is a bit confusing as it's not coming across clearly what you're asking for or trying to do, but "hasNext()" merely returns true or false if there is a record that matches the query.
So if hasNext() is true, then it'll execute that block of code, if hasNext() is false, it will not.
So for your background script example, you also have improper formatting with this line:
inactiveUsers.addQuery('sys_id=62826bf03710200044e0bfc8bcbe5df1');// This is active user id
as the correct formatting is:
inactiveUsers.addQuery('sys_id','62826bf03710200044e0bfc8bcbe5df1');// This is active user id
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2023 12:11 PM
Hello,
My apologies, your post is a bit confusing as it's not coming across clearly what you're asking for or trying to do, but "hasNext()" merely returns true or false if there is a record that matches the query.
So if hasNext() is true, then it'll execute that block of code, if hasNext() is false, it will not.
So for your background script example, you also have improper formatting with this line:
inactiveUsers.addQuery('sys_id=62826bf03710200044e0bfc8bcbe5df1');// This is active user id
as the correct formatting is:
inactiveUsers.addQuery('sys_id','62826bf03710200044e0bfc8bcbe5df1');// This is active user id
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2023 12:30 PM
Hi Sir,
i need to understand below mention code how it is fetching the inactive records. This is out of box code which is write in script include (KBWorkflowSNC).
This code is having 2 addquery (one is checking inactive records and 2nd one is checking all user who are stored in user array.
my question is that how if condition is working with hasNext()
var inactiveUsers = new GlideRecord('sys_user');
inactiveUsers.addQuery("active", "false");
inactiveUsers.addQuery('sys_id', 'IN', users);
inactiveUsers.query();
//Check for any inactiveusers.
if (inactiveUsers.hasNext()) {
var userArray = users.split(",");
while (inactiveUsers.next()) {
var useridx = userArray.indexOf(inactiveUsers.getUniqueValue()+'');
if (useridx != -1)
userArray.splice(useridx, 1);
}
users = userArray.toString();
}
return users;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2023 01:42 PM
In line 3, 'users' is not defined. what do you see if you add "gs.info('users = ' + users);" before or just after line 3?