ServiceNow Learning 64: Functionality of _next & _query operations in ServiceNow

Shamma Negi
Kilo Sage
Kilo Sage

Hi All,

 

Here we will see how the _next and _query functions are used. How they are different from next & query operations.

Please have a look.

 

1.  _next()

Moves to the next record in the Glide Record. Provides the same functionality as next(), intended to be used in cases where the Glide Record has a column named next.
 

Example

 

 

var rec = new GlideRecord('sys_template');
rec.query();
while (rec._next()) { 
  gs.print(rec.number + ' exists');
}
 
 
2. 

_query(String field, String value)

Runs a query against the table based on the filters specified by query methods such as addQuery() and addEncodedQuery(). This method is intended to be used on tables in which there is a column named "query", which might cause errors running the query () method.

Example

The following shows how query the Knowledge Feedback [kb_feedback] table and list KB articles with comments that include Excel.

 

var rec = new GlideRecord('kb_feedback');
rec.addQuery('comments', 'CONTAINS', 'Excel');
rec._query();
while (rec.next()) { 
 gs.info(rec.getDisplayValue('article') + " comment: " + rec.getValue('comments'));
}

Output

 

KB0000005 comment: 
	     Can you please add the version of Excel this applies to? All?
		
KB0000005 comment: 
	     Does this work for all Excel versions? OSX and Windows alike?
 
 
Hope this helps.
I hope this article helpful. Please mark it as helpful and bookmark if you like it.
 
Regards,
Shamma
 
Regards,Shamma Negi
1 REPLY 1

ChristianFX
Tera Contributor

Hello Shamma, 

 

I thought the _next() and _query() methods were desiged to bypass security checks when used in conjuction with the GlideRecordSecure.

 

Also while iterating through records, the method next() should  not confilct with a field  named "next" as those are two diffrent components (methods, class attrribute) . I always thought that ServiceNow should distinguish those both compnents automatically.