What's the difference b/w next() and hasNext()?

jamesgo
Giga Contributor

if the query has only one match, will hasNext() return true?

11 REPLIES 11

Brad Tilton
ServiceNow Employee

hasNext() either returns true or false while next() will actually iterate to the record.



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.


Hi Brad, in 2nd scenario, gr doesn't iterate to the actual record? if there is only one match, what is contained in the gr when using hasNext?


When I was try with above code in nest() if I got the updated record but not updated in case of hasNext().

Hey Brad, Very clear presentation via code; 
I like to add performance & business context to both hasNext() & next() methods
Performance: 
hasNext() : best - very efficient & light on system.
next(): Heavy on system.
Business Context: 
1. Do we have any p1 - Incident not resolved this morning - hasNext()
well, you are just looking for YES or NO answer, so that you can inform the team to prioritize p1.
2. who is working on the p1-incident - next()
you can get the Engineer details who is handling the situation.