GlideRecord Basics - Complete Study Guide
Why GlideRecord is Important
GlideRecord is the fundamental server-side JavaScript API used to interact with the ServiceNow database. Understanding GlideRecord is essential for the CSA exam because it forms the backbone of all data operations in ServiceNow, including querying, inserting, updating, and deleting records. Nearly every business rule, script include, and scheduled job uses GlideRecord to manipulate data.
What is GlideRecord?
GlideRecord is a JavaScript class that provides methods to perform database operations on ServiceNow tables. It acts as an object-oriented wrapper around SQL queries, allowing developers to interact with the database using familiar JavaScript syntax rather than writing raw SQL statements.
Key characteristics of GlideRecord:
- Server-side API (runs on the ServiceNow server)
- Object-oriented approach to database operations
- Handles one record at a time through iteration
- Provides methods for CRUD operations (Create, Read, Update, Delete)
How GlideRecord Works
Basic Query Structure:
1. Initialize - Create a new GlideRecord object for a specific table
2. Add Query Conditions - Specify what records to retrieve
3. Execute Query - Run the query against the database
4. Iterate Results - Loop through returned records
5. Perform Operations - Read, modify, or delete records
Common Methods:
addQuery(field, operator, value) - Adds a condition to filter records
query() - Executes the query and retrieves matching records
next() - Moves to the next record in the result set, returns true if a record exists
get(sys_id) - Retrieves a single record by its sys_id
insert() - Creates a new record in the database
update() - Saves changes to the current record
deleteRecord() - Removes the current record
getValue(field) - Returns the value of a specified field
setValue(field, value) - Sets the value of a specified field
getRowCount() - Returns the total number of records matching the query
Query Operators:
= (equals), != (not equals), > (greater than), < (less than), >= (greater than or equal), <= (less than or equal), CONTAINS, STARTSWITH, ENDSWITH, IN, NOT IN
Example Code Patterns
Querying Records:
var gr = new GlideRecord('incident');
gr.addQuery('priority', '1');
gr.query();
while (gr.next()) {
// Process each high priority incident
}
Getting a Single Record:
var gr = new GlideRecord('incident');
if (gr.get('sys_id_value')) {
// Record found, process it
}
Inserting a Record:
var gr = new GlideRecord('incident');
gr.initialize();
gr.setValue('short_description', 'New incident');
gr.insert();
Exam Tips: Answering Questions on GlideRecord Basics
1. Remember the order of operations - Always create the GlideRecord object first, add query conditions, call query(), then iterate with next() or use get() for single records.
2. Understand the difference between get() and query() - get() retrieves a single record by sys_id and returns true/false. query() retrieves multiple records and requires next() to iterate.
3. Know when to use update() vs insert() - update() modifies existing records, insert() creates new ones. Using update() on a new record or insert() on an existing record will cause errors.
4. Watch for common pitfalls - Questions may include code that forgets to call query() before next(), or attempts to access fields outside the while loop.
5. Recognize addEncodedQuery() - This method accepts a single string containing filter conditions, often copied from a list filter URL.
6. Pay attention to scope - GlideRecord is a server-side API. For client-side operations, look for GlideAjax or GlideRecord used within client scripts (which would be incorrect usage).
7. Understand deleteMultiple() vs deleteRecord() - deleteMultiple() removes all matching records at once, while deleteRecord() removes one record at a time within a loop.
8. Review setLimit() usage - This method restricts the number of records returned, useful for performance optimization.