Basic Syntax:
new GlideQuery('<table_name>')
1. Selecting Fields
- Use
.select()
to choose which fields to retrieve:
<code>
new GlideQuery('incident') .select('number', 'short_description') .forEach(function(record) { gs.info(record.number + ' - ' + record.short_description); });
2. Simple Conditions
- Use
.where()
to filter records:
<code>
new GlideQuery('incident') .where('priority', 1) // Priority = 1 .select('number') .forEach(function(record) { gs.info('High Priority Incident: ' + record.number); });
- Use multiple conditions:
<code>
new GlideQuery('incident') .where('priority', 1) .where('state', '!=', 6) // Not closed .select('number', 'short_description') .forEach(function(record) { gs.info(record.number + ' - ' + record.short_description); });
3. Chaining Conditions
- Chain
.orWhere()
for OR conditions:
<code>
new GlideQuery('incident') .where('priority', 1) .orWhere('urgency', 1) // Priority = 1 OR Urgency = 1 .select('number') .forEach(function(record) { gs.info('Urgent/High Priority Incident: ' + record.number); });
4. Sorting
- Use
.orderByAsc()
or.orderByDesc()
to sort results:
<code>
new GlideQuery('incident') .where('priority', 1) .orderByDesc('number') // Sort by descending number .select('number') .forEach(function(record) { gs.info('Incident: ' + record.number); });
5. Aggregation Functions
- Use
.count()
to get the number of records:
<code>
var count = new GlideQuery('incident') .where('priority', 1) .count(); gs.info('High Priority Incidents Count: ' + count);
- Use
.max()
to get the maximum value of a field:
<code>
var maxNumber = new GlideQuery('incident') .max('number'); gs.info('Highest Incident Number: ' + maxNumber);
6. Limit and Offset
- Use
.limit()
to limit the number of returned results:
<code>
new GlideQuery('incident') .where('priority', 1) .limit(5) // Limit to 5 results .select('number') .forEach(function(record) { gs.info('Incident: ' + record.number); });
7. Joins (Related Records)
- Use
.join()
to query related tables:
<code>
new GlideQuery('incident') .where('priority', 1) .join('caller_id', 'sys_user') // Join to sys_user table on caller_id .select('incident.number', 'sys_user.name') .forEach(function(record) { gs.info('Incident: ' + record.number + ' - Caller: ' + record.sys_user.name); });
8. Updating Records
- Use
.update()
to modify records:
new GlideQuery('incident') .where('priority', 1) .update({ urgency: 2 // Update urgency to 2 for high-priority incidents });
9. Deleting Records
- Use
.delete()
to delete records:
<code>
new GlideQuery('incident') .where('state', 6) // Closed incidents .delete();
10. Query with Subselect
- Subselect allows embedding queries within queries:
<code>
new GlideQuery('incident') .where('caller_id', new GlideQuery('sys_user') .where('active', true) .select('sys_id') ) .select('number') .forEach(function(record) { gs.info('Active User Incident: ' + record.number); });
11. Handling Dates
- Use
.whereBetween()
for date ranges:
<code>
new GlideQuery('incident') .whereBetween('opened_at', '2023-01-01', '2023-12-31') .select('number', 'opened_at') .forEach(function(record) { gs.info(record.number + ' opened at ' + record.opened_at); });
12. First or Single Record
- Use
.first()
to get only the first matching record:
var record = new GlideQuery('incident') .where('priority', 1) .select('number') .first(); if (record) { gs.info('First High Priority Incident: ' + record.number); }
Key Points:
- Functional: GlideQuery uses a functional approach that allows for cleaner, chainable methods.
- Immutable: Each operation creates a new instance of GlideQuery, so queries can be composed more cleanly.
- Efficient: Minimizes the amount of code required to perform complex queries.
GlideQuery Cheat Sheet for ServiceNow
Working Code Asked question September 17, 2024