What is GlideRecord?

GlideRecord is the primary API for database operations in ServiceNow. It allows you to query, insert, update, and delete records from database tables.

Critical for Exams

GlideRecord is the MOST tested topic in CSA and CAD exams. Master query(), next(), getValue(), setValue(), insert(), update(), and deleteRecord().

Query Records - Reading Data

Basic Query Pattern
// Step 1: Create GlideRecord object
var gr = new GlideRecord('incident');

// Step 2: Add query conditions
gr.addQuery('active', true);
gr.addQuery('priority', '1');

// Step 3: Execute query
gr.query();

// Step 4: Loop through results
while (gr.next()) {
    // Step 5: Access field values
    gs.info('Incident: ' + gr.number + ', Priority: ' + gr.priority);
}
Query Operators
var gr = new GlideRecord('incident');

// Equals
gr.addQuery('state', '2');

// Not equals
gr.addQuery('state', '!=', '7');

// Greater than, less than
gr.addQuery('priority', '<', '3');
gr.addQuery('sys_created_on', '>', '2024-01-01');

// IN operator
gr.addQuery('state', 'IN', '1,2,3');

// LIKE (contains)
gr.addQuery('short_description', 'CONTAINS', 'email');

// STARTSWITH, ENDSWITH
gr.addQuery('number', 'STARTSWITH', 'INC');

// IS EMPTY, IS NOT EMPTY
gr.addQuery('assignment_group', 'ISEMPTY');
gr.addQuery('assigned_to', 'ISNOTEMPTY');

gr.query();
OR Conditions
var gr = new GlideRecord('incident');

// OR query - priority 1 OR 2
var qc = gr.addQuery('priority', '1');
qc.addOrCondition('priority', '2');

// Complex OR: (priority=1 OR priority=2) AND state=2
var qc = gr.addQuery('priority', '1');
qc.addOrCondition('priority', '2');
gr.addQuery('state', '2');

gr.query();
while (gr.next()) {
    gs.info(gr.number);
}
Best Practice

Always call query() before next(). Limit results with setLimit() for performance. Use addEncodedQuery() for complex queries copied from filter navigator.

Get/Set Field Values

getValue() and setValue()
var gr = new GlideRecord('incident');
gr.get('sys_id_here');

// Get field value
var priority = gr.getValue('priority');  // Returns string
var state = gr.state.toString();  // Also returns string

// Get reference field sys_id
var assignedToID = gr.getValue('assigned_to');

// Get display value (for reference fields)
var assignedToName = gr.getDisplayValue('assigned_to');
var groupName = gr.assignment_group.getDisplayValue();

// Set field value
gr.setValue('priority', '1');
gr.priority = '2';  // Alternative syntax

// Set reference field
gr.setValue('assigned_to', 'user_sys_id_here');
gr.assigned_to = 'user_sys_id_here';

// Update record
gr.update();

Insert Records

Creating New Records
// Create new incident
var gr = new GlideRecord('incident');
gr.initialize();  // Optional but recommended

gr.short_description = 'Network outage in Building A';
gr.category = 'network';
gr.priority = '1';
gr.caller_id = gs.getUserID();

// Insert returns sys_id of new record
var newSysID = gr.insert();

if (newSysID) {
    gs.info('Created incident: ' + gr.number);
} else {
    gs.error('Failed to create incident');
}

Update Records

Updating Existing Records
// Update single record by sys_id
var gr = new GlideRecord('incident');
if (gr.get('sys_id_here')) {
    gr.state = '2';  // In Progress
    gr.assigned_to = gs.getUserID();
    gr.update();
}

// Update multiple records
var gr = new GlideRecord('incident');
gr.addQuery('state', '1');  // New
gr.addQuery('priority', '1');
gr.query();

while (gr.next()) {
    gr.state = '2';
    gr.assignment_group = 'high_priority_group_id';
    gr.update();  // Update each record
}

// updateMultiple() - updates all matching records
var gr = new GlideRecord('incident');
gr.addQuery('state', '1');
gr.addQuery('priority', '5');
gr.setValue('state', '7');  // Closed
gr.updateMultiple();  // Updates all at once
Best Practice

Use update() in a loop for complex logic. Use updateMultiple() for simple bulk updates (faster, but doesn't run business rules for each record).

Delete Records

Deleting Records
// Delete single record
var gr = new GlideRecord('incident');
if (gr.get('sys_id_here')) {
    gr.deleteRecord();
}

// Delete multiple records
var gr = new GlideRecord('incident');
gr.addQuery('state', '7');  // Closed
gr.addQuery('sys_created_on', '<', 'javascript:gs.daysAgo(365)');
gr.query();

while (gr.next()) {
    gr.deleteRecord();
}

// deleteMultiple() - deletes all matching records
var gr = new GlideRecord('incident');
gr.addQuery('state', '7');
gr.addQuery('sys_created_on', '<', 'javascript:gs.daysAgo(730)');
gr.deleteMultiple();

Important GlideRecord Methods

Essential Methods
var gr = new GlideRecord('incident');

// get() - Query by sys_id, returns boolean
if (gr.get('sys_id_here')) {
    // Record found
}

// get(name, value) - Query by any field
if (gr.get('number', 'INC0010001')) {
    // Record found
}

// getRowCount() - Count results
gr.addQuery('active', true);
gr.query();
var count = gr.getRowCount();
gs.info('Active incidents: ' + count);

// hasNext() - Check if more records
gr.query();
if (gr.hasNext()) {
    // At least one record exists
}

// next() - Move to next record
while (gr.next()) {
    // Process each record
}

// setLimit() - Limit results
gr.setLimit(10);
gr.query();

// orderBy() and orderByDesc()
gr.orderBy('priority');
gr.orderByDesc('sys_created_on');
gr.query();

// nil() - Check if field is empty
if (gr.assignment_group.nil()) {
    // Field is empty
}

// changes() - Check if field was modified
if (gr.priority.changes()) {
    gs.info('Priority changed from ' + gr.priority.getOldValue() + 
            ' to ' + gr.priority.getValue());
}

// changesFrom() and changesTo()
if (gr.state.changesFrom('1').changesTo('2')) {
    // State changed from New to In Progress
}

CSA & CAD Exam Questions (20 Questions)

What method executes a GlideRecord query? Q1
CSA
  • A) gr.execute()
  • B) gr.query()
  • C) gr.run()
  • D) gr.get()
Show Answer
Correct Answer: B
query() executes the query and retrieves matching records. Always call query() before using next().
How do you loop through GlideRecord results? Q2
CSA
  • A) for (gr in results)
  • B) while (gr.hasNext())
  • C) while (gr.next())
  • D) gr.forEach()
Show Answer
Correct Answer: C
Use while (gr.next()) to iterate through query results. next() moves to the next record and returns true if a record exists.
Which method retrieves a single record by sys_id? Q3
CSA
  • A) gr.query('sys_id')
  • B) gr.get('sys_id')
  • C) gr.getByID('sys_id')
  • D) gr.find('sys_id')
Show Answer
Correct Answer: B
gr.get('sys_id') retrieves a single record by sys_id and returns true if found. Example: if (gr.get('123456')) { }
How do you create an OR condition in GlideRecord? Q4
CAD
  • A) gr.addQuery('field', 'value1 OR value2')
  • B) var qc = gr.addQuery('field', 'value1'); qc.addOrCondition('field', 'value2')
  • C) gr.addOrQuery('field', 'value1', 'value2')
  • D) gr.addQuery('field', 'value1') || gr.addQuery('field', 'value2')
Show Answer
Correct Answer: B
Store addQuery() result and call addOrCondition() on it: var qc = gr.addQuery('priority', '1'); qc.addOrCondition('priority', '2');
What does gr.insert() return? Q5
CSA
  • A) true or false
  • B) The sys_id of the new record
  • C) The record number
  • D) The GlideRecord object
Show Answer
Correct Answer: B
insert() returns the sys_id of the newly created record. Store it: var newID = gr.insert();
What's the difference between update() and updateMultiple()? Q6
CAD
  • A) No difference
  • B) update() runs business rules for each record, updateMultiple() doesn't
  • C) updateMultiple() is slower
  • D) update() can only update one field
Show Answer
Correct Answer: B
update() in a loop runs business rules for each record. updateMultiple() updates all records at once without running business rules per record (faster but less control).
How do you get the display value of a reference field? Q7
CSA
  • A) gr.getValue('assigned_to')
  • B) gr.getDisplayValue('assigned_to')
  • C) gr.assigned_to.getName()
  • D) gr.getReference('assigned_to')
Show Answer
Correct Answer: B
getDisplayValue() returns the display value (name) of reference fields. getValue() returns the sys_id.
Which method counts the number of records returned by a query? Q8
CSA
  • A) gr.count()
  • B) gr.getRowCount()
  • C) gr.size()
  • D) gr.length()
Show Answer
Correct Answer: B
getRowCount() returns the number of records. Call it after query(): gr.query(); var count = gr.getRowCount();
How do you check if a field is empty in GlideRecord? Q9
CSA
  • A) if (gr.field == '')
  • B) if (gr.field.nil())
  • C) if (gr.isEmpty('field'))
  • D) if (!gr.field)
Show Answer
Correct Answer: B
Use .nil() to check if a field is empty: if (gr.assignment_group.nil()) { }. This is the ServiceNow recommended method.
What's the correct order for GlideRecord operations? Q10
CSA
  • A) new GlideRecord → next() → addQuery() → query()
  • B) new GlideRecord → query() → addQuery() → next()
  • C) new GlideRecord → addQuery() → query() → next()
  • D) new GlideRecord → addQuery() → next() → query()
Show Answer
Correct Answer: C
Correct order: 1) new GlideRecord 2) addQuery 3) query() 4) next(). Always add queries before calling query(), then iterate with next().
Which method limits the number of records returned? Q11
CAD
  • A) gr.limit(10)
  • B) gr.setLimit(10)
  • C) gr.setMaxRows(10)
  • D) gr.take(10)
Show Answer
Correct Answer: B
setLimit(n) limits query results. Call before query(): gr.setLimit(10); gr.query();
How do you sort GlideRecord results in ascending order? Q12
CSA
  • A) gr.sort('priority')
  • B) gr.orderBy('priority')
  • C) gr.sortBy('priority')
  • D) gr.order('priority')
Show Answer
Correct Answer: B
orderBy('field') sorts ascending. orderByDesc('field') sorts descending. Call before query().
What does gr.changes() check? Q13
CAD
  • A) If any field in the record changed
  • B) If a specific field was modified
  • C) The change history
  • D) Who made changes
Show Answer
Correct Answer: B
gr.field.changes() returns true if that specific field was modified. Used in business rules with 'current'.
Can you use GlideRecord in client scripts? Q14
CSA
  • A) Yes, directly
  • B) No, use GlideAjax instead
  • C) Only in onLoad scripts
  • D) Only for reading data
Show Answer
Correct Answer: B
GlideRecord cannot be used in client scripts (browser). Use GlideAjax to call server-side Script Includes that use GlideRecord.
What's the purpose of gr.initialize()? Q15
CAD
  • A) Start the query
  • B) Prepare a new record with default values
  • C) Reset the GlideRecord
  • D) Initialize the database connection
Show Answer
Correct Answer: B
initialize() prepares a new record and sets default values defined in the dictionary. Use before insert(): gr.initialize(); gr.field = value; gr.insert();
How do you use addEncodedQuery()? Q16
CAD
  • A) To encode special characters
  • B) To add a complex query string from filter navigator
  • C) To encrypt the query
  • D) To format query output
Show Answer
Correct Answer: B
addEncodedQuery() accepts an encoded query string from the filter navigator. Right-click breadcrumb → Copy query. Example: gr.addEncodedQuery('active=true^priority=1');
What does gr.canWrite() check? Q17
CAD
  • A) If the record is writable to disk
  • B) If the current user has write access to the record
  • C) If the field is readonly
  • D) If the record can be saved
Show Answer
Correct Answer: B
canWrite() returns true if the current user has write access based on ACLs. Also available: canRead(), canDelete(), canCreate().
How do you query for records with CONTAINS operator? Q18
CSA
  • A) gr.addQuery('description', 'email')
  • B) gr.addQuery('description', 'CONTAINS', 'email')
  • C) gr.addQuery('description', 'LIKE', '%email%')
  • D) gr.contains('description', 'email')
Show Answer
Correct Answer: B
Use three-parameter addQuery with 'CONTAINS': gr.addQuery('short_description', 'CONTAINS', 'email'). Other operators: STARTSWITH, ENDSWITH, IN, etc.
What's the difference between setValue() and field assignment? Q19
CAD
  • A) No difference
  • B) setValue() bypasses certain business logic
  • C) Direct assignment is faster
  • D) setValue() is deprecated
Show Answer
Correct Answer: A
Both work the same: gr.setValue('priority', '1') and gr.priority = '1' are equivalent. Use whichever is more readable.
How do you get the previous value of a field that changed? Q20
CAD
  • A) gr.field.previous()
  • B) gr.field.oldValue()
  • C) gr.field.changes().getOldValue()
  • D) current.field.getOldValue()
Show Answer
Correct Answer: D
In business rules, use current.field.changes() to check if changed, then current.field.getOldValue() to get previous value. Example: if (current.priority.changes()) { var old = current.priority.getOldValue(); }

GlideRecord Cheat Sheet

  • new GlideRecord('table')
  • addQuery(field, value)
  • query() - Execute
  • next() - Loop
  • get(sys_id) - Single record
  • getValue()/setValue()
  • insert()/update()/deleteRecord()
  • getRowCount() - Count
  • nil() - Check empty
  • changes() - Check modified