Difference between query() and _query() , next() and _next()

SeemaJenitT
Tera Contributor
 
8 REPLIES 8

Hello @SeemaJenitT  ,

 

Just wanted to check in to see if my earlier response helped you out or if you're still facing any issues. If your query is resolved, it would be great if you could mark my reply as helpful and accept it as the solution — that way it can also benefit others who come across the thread later.

 

Also, if you found any other responses helpful, feel free to mark those as helpful too. You can even accept multiple solutions if they contributed to resolving your issue.

 

Let me know if you need anything else!

 

Best regards,
Aniket Chavan
ServiceNow MVP 2025 | ServiceNow Rising Star 2024

Hello @SeemaJenitT  ,

 

Just wanted to check in to see if my earlier response helped you out or if you're still facing any issues. If your query is resolved, it would be great if you could mark my reply as helpful and accept it as the solution — that way it can also benefit others who come across the thread later.

 

Also, if you found any other responses helpful, feel free to mark those as helpful too. You can even accept multiple solutions if they contributed to resolving your issue.

 

Let me know if you need anything else!

 

Best regards,
Aniket Chavan
ServiceNow MVP 2025 | ServiceNow Rising Star 2024

Bhimashankar H
Mega Sage

Hey @SeemaJenitT ,

 

I have summarized the difference between these 4 methods for you. See table below.

When working with GlideRecord in ServiceNow, you may encounter both standard methods and "underscore" methods. These serve similar purposes with subtle but important differences

 

 Method     Standard Use                          When to Use Underscore Version

query()Standard record retrievalIf your table has a field named query
_query()Same as query()To avoid conflict with a query field
next()Standard record iterationIf your table has a field named next
_next()Same as next()To avoid conflict with a next field

 

Always use the standard query() and next() unless you specifically have fields named query or next in your table schema—in those cases, use _query() and _next() to prevent script errors and ensure reliable operation.

 

1. query()

var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.query();
while (gr.next()) {
  // process records
}

 

2. _query()

var gr = new GlideRecord('custom_table');
gr._query(); //you will see this method used in OOTB widgets whenver they query on the tables.
while (gr.next()) {
  // process records
}

 

3. next()

while (gr.next()) {
  // process the current record
}

 

4. _next()

while (gr._next()) {
  // process records safely even with a 'next' field
}

 

Thanks,
Bhimashankar H

 

---------------------------------------------------------------------------------------------------------
If my answer helps you or resolves your query, please consider marking as 'Helpful'/'Accept As Solution'. So future readers with similar questions can find it easily. Your coordination will help community to grow stronger!.
----------------------------------------------------------------------------------------

Bhimashankar H
Mega Sage

@SeemaJenitT ,

 

I hope you saw my reply. 
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. It will help future readers as well having similar kind of questions and close the thread.

 

Thanks,
Bhimashankar H