When do we use next(), hasNext() and _next()

chanikya
Kilo Sage

Hi All,

I have 10 records only on whole table , now I want to update records So 

When do we use next(), hasNext() and _next()

1 ACCEPTED SOLUTION

Hi,

just to check if there is any record satisfying the query

hasNext() - does not actually go to that record but just tells true/false is there is any record matching the query

Regards
Ankur

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

View solution in original post

25 REPLIES 25

while loop will only run till gr.next() record is not null. So there is no need to use hasNext() 

while(gr.next())

Hi,

it's correct syntax

Regards
Ankur

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

@chanikya 

I could see you marked my response as correct earlier and now it is not.

Would you mind marking my response as correct if I was able to help you.

Regards
Ankur

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

Community Alums
Not applicable

Hi Chanikya,

 

hasNext() : hasNext() method returns true if iterator have more elements.

next() : next() method returns the next element and also moves to the next element.

_next() : is method similar to next() in GlideRecord.

Moves to the next record in the GlideRecord. Provides the same functionality as next(), intended to be used in cases where the GlideRecord has a column named next.

Example:- 

So I could do:

 

gr.query();

if (gr.next()) {
 gr.field = 'value';
gr.update();}

but this won't update anything:

gr.query();

 

if (gr.hasNext()) {

 

  gr.field = 'value';

 

  gr.update();}

So if there are one or more records contained in your gr set, hasNext() will return true.


Please mark my reply as Correct & Helpful, if applicable.

Thanks

Sandeep

if we want to use hasnext() in script then do we have to use like below?

gr.query()
while(gr.hasNext())
{
if(gr.next())
{

gr.description='welcome';

gr.update();

}}