- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2022 07:53 AM
I have a script like below
var rec = new GlideRecord('incident');
rec.query();
if(rec.next()) {
gs.info(rec.getValue('number') + ' exists');
}
Is "rec.next ()" necessary even if there is only one record in the table?
I've used it somehow, but I don't understand how it works.
https://developer.servicenow.com/dev.do#!/reference/api/paris/server/c_GlideRecordScopedAPI
At the above site, "Go to the next record of the GlideRecord object. There was an explanation.
I thought .next wouldn't be needed for a table with only one Record.
However, I am confused because I get an error with the following code that I deleted.
var rec = new GlideRecord('incident');
rec.query();
gs.info(rec.getValue('number') + ' exists');
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2022 03:31 PM
ServiceNow is probably using jdbc so the operation of .next() seems the same. Just doing a query won't move the cursor before the first record so it's necessary to do a .next() to move the cursor to the first record.
Check the following jdbc document on ResultSet.
Initially the cursor is positioned before the first row. The next
method moves the cursor to the next row, and because it returns false
when there are no more rows in the ResultSet
object, it can be used in a while
loop to iterate through the result set.
https://docs.oracle.com/javase/8/docs/api/java/sql/ResultSet.html
Doing "if (gr.next())" also ensures that there's is at least one record in the table. Without doing this, the script will error when there's no record. Without .next(), this check won't be possible.
To just one record, if a key to the record is known, it's possible to use a .get() instead of a query() and an .next(). The example is using 'sys_id', but it can be other field. In the case of sys_id,
var rec = new GlideRecord('incident');
if (rec.get('sys_id','<sys_id of incident record to retrieve>')) {
gs.info(rec.getElement('number') + ' exists');
}
In case of retrieving using sys_id column, column name 'sys_id' can be omitted so .get('<sys_id>') will retrieve the record with the specified sys_id.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2022 08:06 AM
Hi,
.next() is necessary as that is how you net to the next( or first) record of the result set.
Also, one thing i picked over the years, if your table has a field called next, then the system gets confused and in that case you can use ._next() , it works the same way. _next() can be sued even if there is not field called next on the table.
Hope this helps.
-Anurag
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2022 03:40 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2022 03:31 PM
ServiceNow is probably using jdbc so the operation of .next() seems the same. Just doing a query won't move the cursor before the first record so it's necessary to do a .next() to move the cursor to the first record.
Check the following jdbc document on ResultSet.
Initially the cursor is positioned before the first row. The next
method moves the cursor to the next row, and because it returns false
when there are no more rows in the ResultSet
object, it can be used in a while
loop to iterate through the result set.
https://docs.oracle.com/javase/8/docs/api/java/sql/ResultSet.html
Doing "if (gr.next())" also ensures that there's is at least one record in the table. Without doing this, the script will error when there's no record. Without .next(), this check won't be possible.
To just one record, if a key to the record is known, it's possible to use a .get() instead of a query() and an .next(). The example is using 'sys_id', but it can be other field. In the case of sys_id,
var rec = new GlideRecord('incident');
if (rec.get('sys_id','<sys_id of incident record to retrieve>')) {
gs.info(rec.getElement('number') + ' exists');
}
In case of retrieving using sys_id column, column name 'sys_id' can be omitted so .get('<sys_id>') will retrieve the record with the specified sys_id.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2022 03:41 PM