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

Hey Upender,

3 records to update INC001001,INC001002, INC001003 

var gr=new GlideRecord('incident');

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

gr.query();

while(gr.hasNext()) ---> when it is checking with 1001, it will see is there any record after 1001 right ? so almost we have still 2 records , So Condition TRUE.

But when it is checking 1003 it will see is there any record after 1003, but there is no record after 1003 then it gives FALSE. so once result is FALSE then it will not comes to if condition 

{

if(gr.next())

{

in this case what about 1003.update ?

 

  • Hi Chanikya, Let me just make it simple for you 

    lets take table incident as example and when we use a glide record on it  , we are trying to filter out some records from the incident table , during this time,
    1) next() = This can be used to go to each record that we fetched after the filter condition , this next() will let you to go to each record and update any value in that record 
    2) hasNext() = while  hasNext() will just check and say whether there are records present or not based on the filter conditions , they just check if there is any record present but does not return the reference to that record , while next() will check if any record is there next and also return reference to that record , so since we have reference to a record by using next() we can do updates do that record .

Ujjawal Vishnoi
Mega Sage
Mega Sage

Hi Chanikya,

1. next() is used in while and if and because of this we can get object within curly bracket {} of while/if and hence we can use gr.fieldName.

2. hasNext() is used in if and it checks whether we have any record as per addQuery or not. We can not get object within curly bracket {} of if and hence we can not use gr.fieldName in this method. It is generally used when we want to perform some action if query has any record and within curly bracket of if you do not want to use gr.fieldName as it will not be available as available in next() method.

3. _next() is method similar to next() in GlideRecord. It moves to the next record in the GlideRecord. Provides the same functionality as next(), intended to be used in cases where the table has a column named next.

 

Hope it helps.

Regards,

Ujjawal

Upender Kumar
Mega Sage

1. next() is used to get the next record

2. hasNext() check the next record and return true/false

3. _next() is used where the table already has a column name as next.

Allen Andreas
Administrator
Administrator

Hi,

This is why it's very important for you to learn what is going on here. It's great that there's been so many replies here of people basically saying the same thing, but in reality, you've been answered as to what each term means. Now...test it yourself?

There's a lot of speculation going on and it can easily be solved by you simply create a small background script and then print the results just to see what would happen, limiting it to even 3 records as you've mentioned INC*1, INC*2, INC*3, etc.

Conducting some hands-on practice I think will quickly get you informed.

Best of luck!

Please mark reply as Helpful, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!