Is it possible to iterate through a GlideRecord query twice

jasonkist
Giga Guru

Hi all, was wondering if it's possible to iterate through a GlideRecord twice. This is just an example I whipped up, wondering if resetting the iterator is possible.

E.G.

var user = new GlideRecord('sys_user');

user.addQuery('user_name','jason');

user.query();

while(user.next()){

//do stuff

}

user.functionToResetIterator(); //is there functionality to do this??

while(user.next()){

//do other stuff

}

 

1 ACCEPTED SOLUTION

Anil Lande
Kilo Patron

You can do something like this:

var user = new GlideRecord('sys_user');
user.addQuery('user_name','jason');
user.query();
while(user.next()){
function1(user);
function2(user);
}

function1: function(user){
// do function1 stuff
}

function2: function(user){
//do other stuff
}

 

Thanks,

Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

4 REPLIES 4

Saurav11
Kilo Patron
Kilo Patron

Hello,

Yes it is possible:-

Please find a below thread related to same.

https://community.servicenow.com/community?id=community_question&sys_id=b6285411db99d890190dfb24399619df

Please mark answer correct/helpful based on impact.

I read through this yesterday while researching. To me, it's not possible to iterate twice. Your answer says 'yes, it is possible' but I didn't see a solution on how to do this. Am I missing something?

Anil Lande
Kilo Patron

Hi,

Instead of reiterating it again you can put logic in same while block.

Or use same GlideRecord query again to get same records.

 

 

Thanks,

Anil Lande

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Anil Lande
Kilo Patron

You can do something like this:

var user = new GlideRecord('sys_user');
user.addQuery('user_name','jason');
user.query();
while(user.next()){
function1(user);
function2(user);
}

function1: function(user){
// do function1 stuff
}

function2: function(user){
//do other stuff
}

 

Thanks,

Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande