- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-08-2020 09:06 AM
What is the difference between if(inc.next()) & while(inc.next()). if ran a query by doing GlideAggregate to get the count of incidents. if i get always some records more than 150 . which one is preferable using if(next()) or using while(next()).
I believe using while loop runs the query for 150 times . Using if is preferable. Am i Correct
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-08-2020 11:01 AM
Hi,
Yes you are correct. While loop code will run for 120 times and if code will run once.
Thanks,
Ashutosh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-08-2020 10:12 AM
for example say there is count of 50 records,
then while loop will run 50 times & get the count as 50 by running 50 times
where are if loop runs only once & get the count as 50 by running 1 time
Am i correct ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-08-2020 11:01 AM
YES !!
But it is recommended to use while if you want to return multiple records.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-08-2020 10:31 AM
Krishna,
According to ServiceNow's documentation using GlideAggregate to get a count, most examples utilize an if statement. According to the Developer article Understanding GlideAggregate there is a significant cost difference between using GlideRecord and .getRowCount() VS GlideAggregate .getAggregate('count').
Most examples show GlideAggregate queries using the if(count.next()) statement. .next() returns true if there are more records in the query set and false if there are no records or it has reached the end.
var count = new GlideAggregate('incident');
count.addAggregate('COUNT');
count.query();
var incidents = 0;
if (count.next()) {
incidents = count.getAggregate('COUNT');
gs.info(incidents);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-08-2020 11:02 AM
After you execute inc.query(), inc.next() is how you iterate to the next record that is "contained" in the GlideRecord. GlideRecords contain lists of records that match the query you set up before executing inc.query(). inc.next() rotates through those records.
In some cases, you want to iterate through every record contained in the query. In that case, you would use a loop to iterate through all of the records "contained" in the GlideRecord: while( inc.next() ).
In both cases, if(inc.next()) and while(inc.next()), the same "inc.next()" call is being used.
When inc.next() is called, an attempt is made to iterate to the next record contained in the GlideRecord. If the attempt to iterate to the next record in the GlideRecord was successful, a "true" will be returned. If the attempt to iterate to the next record "contained" in the GlideRecord was not successful, a "false" will be returned.
So, when you are using "if(inc.next())" or "while(inc.next())", both of those operations are looking to see if inc.next() returns true or false.
With "if(inc.next())", whatever comes in the curly brackets afterwards will be executed if the iteration to the next record "contained" in the GlideRecord was successful:
if( inc.next() ){
//This will get executed if inc.next() returned true (because there was another, "next" record contained in the GlideRecord that I was able to iterate to by calling inc.next()
}
With "while(inc.next())", if inc.next() returns true, that means the conditions have been met to have a while loop continue:
while( inc.next() ){
//Every time the while loop repeats, the conditions will be checked to see if the loop should repeat again. inc.next() will be run each time the while loop checks to see if it should repeat the while loop again. inc.next() will attempt to iterate to the next record "contained" within the GlideRecord. If there is a "next" record that inc.next() was able to iterate over to, then inc.next() will return "true", and the while loop will repeat itself again.
}
Use if(inc.next()){} any time you just want to see if the query contained at least one record. Every time you call inc.next(), an attempt will be made to iterate to the next record contained in the GlideRecord. Because inc.next() is only being called one time in "if( inc.next() )", you will only ever check on the first record returned by the query you set up for the GlideRecord before calling inc.query().
With while(inc.next()), inc.next() will be evaluated each time the while loop is checking to see if it should repeat. The end result is that inc.next() will be called for every single record contained in the GlideRecord.
For GlideAggregates, you don't need to iterate over every single record contained in the GlideRecord. You can just check the first record by calling inc.next() once, and then checking the aggregates that you are interested in, for instance inc.getAggregate('COUNT').
So for a GlideAggregate, you actually don't even need to use "if( inc.next() )", you could just use "inc.next()", since with GlideAggregate, doing one "next()" function kind of just initializes the entire GlideAggregate after executing the query so that you can check on the aggregates that you set up in your pre-query() stuff:
var A = new GlideAggregate('[table name]');
A.addAggregate('COUNT');
A.addQuery([set up whatever query you are interested in]);
A.query();
A.next();
gs.info( A.getAggregate('COUNT') );
Hopefully that helps