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

chanikya
Tera Guru

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

Community Alums
Not applicable

Correct !!

kindly hep me for understanding 

if there is a single record in whole table ? then how will hasNext() will works ?

 

Community Alums
Not applicable

Hi Chanikya,

The basic understanding for hasNext() is usually used to check there's another step to be taken.

If, for example, your query returns three records, you might call .next() after the query to get the first record. At this point, hasNext() will return true. Calling next() again to get the second record will leave us with one left, so once again, hasNext() will return true. However, calling next() one more time will populate the third and final record into the GlideRecord object. There being no further additional records to grab, if we call hasNext() once more at this point, it will return false.

 

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

Thanks

Sandeep

Hi,

Lets we take 3 records to update INC001001,INC001002, INC001003 

var gr=new GlideRecord('incident');

gr.addQuery('category','software');

gr.query();

while(gr.hasNext())

{

if(gr.next())

{

gr.description="ABC";

gr.update();

}

}

REsult :

first level- TRue : INC001001.description =ABC

Second level -True: INC001002.description =ABC

third level- False: INC001003.description=

INC001003 will not be updated.

is it Correct ? 

 

So then how to update INC001003.

 

Yes if query not returning all the 3 records.