Scripting
What is GlideRecord?
The question
What is GlideRecord? Write a basic GlideRecord query to find all open P1 incidents assigned to a specific group.
Setting the Stage
GlideRecord is the single most important API in ServiceNow development. If there's one thing you need to know for any technical interview, it's this. Every developer, admin, and consultant uses GlideRecord daily.
What Is GlideRecord?
GlideRecord is a server-side JavaScript API that lets you query, insert, update, and delete records from any ServiceNow table. It's your primary tool for interacting with the database — like SQL queries, but in JavaScript.
Basic Operations
// === QUERY (SELECT) ===
var gr = new GlideRecord('incident'); // Target the incident table
gr.addQuery('priority', 1); // WHERE priority = 1
gr.addQuery('state', '!=', 6); // AND state != Resolved
gr.addQuery('assignment_group.name', 'Network Support'); // AND group name
gr.orderByDesc('sys_created_on'); // ORDER BY created descending
gr.setLimit(50); // LIMIT 50
gr.query(); // Execute the query
while (gr.next()) { // Iterate through results
gs.info('Incident: ' + gr.getValue('number'));
gs.info('Caller: ' + gr.getDisplayValue('caller_id'));
gs.info('Created: ' + gr.getValue('sys_created_on'));
}
// === INSERT ===
var newInc = new GlideRecord('incident');
newInc.initialize();
newInc.short_description = 'Server Down - Building A';
newInc.priority = 1;
// Look up the caller by user_name to get their sys_id
var caller = new GlideRecord('sys_user');
caller.addQuery('user_name', 'john.smith');
caller.query();
if (caller.next()) {
newInc.caller_id = caller.sys_id;
}
var newSysId = newInc.insert(); // Returns the sys_id of new record
// === UPDATE ===
var gr = new GlideRecord('incident');
gr.get('number', 'INC0010001'); // Get a specific incident
gr.state = 6; // Set state to Resolved
gr.close_notes = 'Fixed by restarting the service';
gr.update(); // Save changes
// === DELETE ===
var gr = new GlideRecord('incident');
gr.get('number', 'INC0010001');
gr.deleteRecord(); // Delete the recordKey Methods to Know
addQuery(field, value)— adds a WHERE conditionaddQuery(field, operator, value)— adds a WHERE with operator (!=, >, <, IN, etc.)addEncodedQuery(string)— adds a complex filter as encoded query stringquery()— executes the querynext()— moves to the next record (returns false when done)getValue(field)— returns the raw database valuegetDisplayValue(field)— returns the display valuegetRowCount()— executes a full COUNT(*) query; avoid on large tables. UseGlideAggregatewithaddAggregate('COUNT')insteadget(field, value)— shortcut to query a single record
💡 Interview Tip
When writing GlideRecord in an interview, always include setLimit() for queries on large tables, always use getValue() instead of dot-notation for field values, and always add if (isLoading) return; at the top if it's inside a Client Script. These small details signal experience.