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

@Marcin Kroszel 

Is this question resolved or you need some more assistance?

Let me know if that answered your question. If so, please mark my response as correct & 👍Helpful so that others can benefit from similar question in future.

Regards
Ankur

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