when should we use hasnext()

sohan_snow
Tera Contributor

Hi All,

I understand that hasnext() will either return true or false, but next() also in a way does the same thing and then goes to the next record.

I would like understand when should we use hasnext(), Is there any case scenario where usage of hasnext() is important

Thanks in advance

1 ACCEPTED SOLUTION

Dave Smith1
ServiceNow Employee
ServiceNow Employee

Although the question has been answered, you may find reading up about an iterator explains a bit about the underlying purpose of these two methods.



Suggested reading: https://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html



For what it's worth, consider standing near the edge of a cliff:


  • hasNext() is usually used to check there's another step to be taken
  • next() takes that step.

View solution in original post

11 REPLIES 11

Chandu Telu
Tera Guru
Tera Guru

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


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


Brad Tilton
ServiceNow Employee
ServiceNow Employee

hasNext() is a little lighter weight since it just returns a true/false where next() actually returns and steps you into the next gliderecord. I would say if your goal is just to determine whether the GlideRecord set has records then use hasNext(), but if you need to manipulate or use values from one or more of the records returned you'll want to use .next().


PriyaRanji
Tera Guru

Hi Sohan,



Good Day



hasNext() - will be used if there are any more records while GlideRecord.



RestAPIRequestBody - hasNext()



Determine if there are additional entries in the request body.


Use this method with the nextEntry() method to iterate over multiple request body entries.



Eg:



var requestBody = request.body;



requestBody.hasNext(); // returns true if the request contains a single entry or multiple entries



//calling second time



requestBody.hasNext(); // returns false if the request contains a single entry, or true if the request contains multiple entries




Thanks,


Priyanka R




Dave Smith1
ServiceNow Employee
ServiceNow Employee

Although the question has been answered, you may find reading up about an iterator explains a bit about the underlying purpose of these two methods.



Suggested reading: https://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html



For what it's worth, consider standing near the edge of a cliff:


  • hasNext() is usually used to check there's another step to be taken
  • next() takes that step.

Yadin
Tera Contributor

I'm finding that hasNext does not work, it returns "undefined" instead of true so that the script can move on.

while(check.next()){

works to move on and do things reliably

if(check.hasNext()){

does not work in weird ways.  In a background script it returns undefined instead the result of the if statement.  This seems to cause the if to evaluate true erratically instead of per the condition being true or false.  Why that evaluation is erratic as well is really baffling.

Thoughts?