GlideRecord - addQuery(String name, Object operator, Object value)

Provides the ability to build a request, which when executed, returns the rows from the specified table, that match the request.

If you are familiar with SQL, this method is similar to the "where" clause. One or more addQuery() calls can be made in a single query; in this case the queries are AND'ed. If any of the query statements need to be OR'ed, use the GlideQueryCondition - Global class.

addQuery() is typically called with three parameters; table field, operator, and comparison value. It can be called with only two parameters, table field and comparison value, such as myObj.addQuery('category','Hardware');. The operator in this case is assumed to be "equal to".

Always test queries on a sub-production instance prior to deploying them on a production instance. An incorrectly constructed encoded query, such as including an invalid field name, produces an invalid query. When the invalid query is run, the invalid part of the query condition is dropped, and the results are based on the valid part of the query, which may return all records from the table. Using an insert(), update(), deleteRecord(), or deleteMultiple() method on bad query results can result in data loss.

You can set the glide.invalid_query.returns_no_rows system property to true to have queries with invalid encoded queries return no records.

Table 1. Parameters
Name Type Description
name String Table field name
operator Object Query operator. The available values are dependent on the data type of the value parameter.
Numbers:
  • =
  • !=
  • >
  • >=
  • <
  • <=
Strings (must be in upper case):
  • =
  • !=
  • IN
  • NOT IN
  • STARTSWITH
  • ENDSWITH
  • CONTAINS
  • DOES NOT CONTAIN
  • INSTANCEOF
value Object Value on which to query (not case-sensitive).
Table 2. Returns
Type Description
QueryCondition A reference to the QueryCondition that was added to the GlideRecord.

Scoped equivalent

To use the addQuery() method in a scoped application, use the corresponding scoped method: Scoped GlideRecord - addQuery(String name, String operator, Object value).

var rec = new GlideRecord('incident');
rec.addQuery('active',true);
rec.addQuery('sys_created_on', ">", "2010-01-19 04:05:00");
rec.query();
while (rec.next()) { 
 rec.active = false;
 gs.print('Active incident ' + rec.number + ' closed');
 rec.update();
}

Using the IN operator.

var que = new GlideRecord('incident');
que.addQuery('number','IN','INC00001,INC00002');
que.query();
while(que.next()) {
 //do something....
}