How hasnext() method is working in script

anilkumarsharma
Giga Guru

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");
}

 

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

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!

View solution in original post

3 REPLIES 3

Allen Andreas
Administrator
Administrator

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!

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;

Bert_c1
Kilo Patron

In line 3, 'users' is not defined. what do you see if you add "gs.info('users = ' + users);" before or just after line 3?