B_35
Tera Contributor

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