Difference between query() and _query() , next() and _next()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2025 10:08 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2025 10:16 PM
Hi @SeemaJenitT ,
Please refer below link for understanding the differences:
https://www.servicenow.com/community/developer-forum/servicenow-learning-64-functionality-of-next-am...
https://www.servicenow.com/community/itsm-forum/when-do-we-use-next-hasnext-and-next/td-p/588445/pag...
If my answer helped you in any way please mark it as correct or helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2025 10:16 PM
Hi @SeemaJenitT ,
Actually there is no difference at all
if you are Querying a table which contains column names (next) you use _next
if the table contains a column names (query) you use _query
these are just alternatives in those special cases
example
in the sys_template table there is a field called next
if you next method in the GlideRecord while querying the sys_template table the gliderecord query fails
in this case you have to use _next
hope this make sense
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2025 10:16 PM - edited 07-20-2025 10:17 PM
Hello @SeemaJenitT
query() vs. _query()
query(): This is the standard and most commonly used method to execute a GlideRecord query. After setting up your filters (e.g., using addQuery(), addEncodedQuery()), you call gr.query() to fetch the matching records from the database.
var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.query(); // Executes the query
while (gr.next()) {
gs.print(gr.number);
}
_query(): This method provides the same functionality as query(). However, it's specifically intended to be used in rare scenarios where the table you are querying has a column (field) named query. If such a field exists, using gr.query() might lead to unexpected behavior or errors because query could be interpreted as the column name rather than the method. Using _query() resolves this ambiguity.
// Example: If 'my_custom_table' has a field named 'query'
var gr = new GlideRecord('my_custom_table');
gr.addQuery('state', 'open');
gr._query(); // Use _query() to avoid conflict with a 'query' field
while (gr._next()) { // Often used with _next() for consistency
gs.print(gr.name);
}
next() vs. _next()
next(): This is the standard and most commonly used method to move the GlideRecord cursor to the next record in the result set. It returns true if there is a next record and moves to it, and false if there are no more records. It's almost always used within a while loop to iterate through all records returned by a query.
var gr = new GlideRecord('incident');
gr.query();
while (gr.next()) { // Moves to the next record and checks if it exists
gs.print(gr.short_description);
}
_next(): Similar to _query(), this method provides the same functionality as next(). It's used in specific cases where the table you are iterating through has a column (field) named next. Using gr._next() prevents potential conflicts or misinterpretations with a field named next.
// Example: If 'another_custom_table' has a field named 'next'
var gr = new GlideRecord('another_custom_table');
gr.query();
while (gr._next()) { // Use _next() to avoid conflict with a 'next' field
gs.print(gr.display_value);
}
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2025 03:50 AM
Hello @SeemaJenitT ,
When working with GlideRecord in ServiceNow, both query() and next() are commonly used methods to fetch and iterate over records. However, in some specific cases, you might come across _query() and _next().
Here's the difference:
-
query() vs _query():
Both are used to execute the query after you’ve added conditions (like addQuery() or addEncodedQuery()), and both behave the same.
The only reason to use _query() is when the table you’re working with happens to have a field named query — in such cases, calling query() may create confusion or unexpected behavior. _query() avoids that conflict. -
next() vs _next():
These are used to move through the records one by one. Again, functionally they behave the same.
Just like with query, if your table has a field named next, then using _next() helps prevent any clash between the method and the field name.
So in most normal use cases, query() and next() will work just fine. But if your table contains columns named query or next, switch to _query() and _next() to avoid issues.
Hope this clears it up.
🔹 Please mark ✅ Correct if this solves your query, and 👍 Helpful if you found the response valuable.
Best regards,
Aniket Chavan
🏆 ServiceNow MVP 2025 | 🌟 ServiceNow Rising Star 2024