Two while loops within one GlideRecord query

Marcin Kroszel
Kilo Guru

Dear Community,

a simple question from a newbie.

I was asked to copy/paste bookmarks from one user to another and I was successful with that, however, I came up with one question.

Why two 'while' loops can't be executed within one GlideRecord query? 

Here is the script. It is supposed to copy/paste the bookmarks and then show me the list of bookmarks belonging to the main user. Only the first 'while' is always executed.

find_real_file.png

Thank you 🙂

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

probably because when the 1st while loop ends the gliderecord object refer to last record for that query and thereafter it won't iterate over again as no records exists for this query

Example below:

var gr = new GlideRecord('incident');
gr.addQuery('caller_id', gs.getUserID());
gr.setLimit(10);
gr.query();

gs.info(gr.getRowCount());

while(gr.next()){
gs.info('First while '+gr.number);

}


gs.info('After 1st while ends '+ gr.number);

while(gr.next()){
gs.info('Second while '+ gr.number);
}

When the 1st while loop ends; it refers to the last record;

So it still prints the INC number After 1st while; but there are no records after this hence it fails to print the logs for 2nd while

Output:

find_real_file.png

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

Regards
Ankur

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

probably because when the 1st while loop ends the gliderecord object refer to last record for that query and thereafter it won't iterate over again as no records exists for this query

Example below:

var gr = new GlideRecord('incident');
gr.addQuery('caller_id', gs.getUserID());
gr.setLimit(10);
gr.query();

gs.info(gr.getRowCount());

while(gr.next()){
gs.info('First while '+gr.number);

}


gs.info('After 1st while ends '+ gr.number);

while(gr.next()){
gs.info('Second while '+ gr.number);
}

When the 1st while loop ends; it refers to the last record;

So it still prints the INC number After 1st while; but there are no records after this hence it fails to print the logs for 2nd while

Output:

find_real_file.png

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

Regards
Ankur

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

as always I much appreciate your support.

Thanks for explanation!

Glad that it helped

Could you also visit your previous question and check if that is answered as well

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

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

Is this answered?

if my answer helped you, kindly mark it as Correct & 👍Helpful so that it does not appear in unanswered list & close the thread.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader