Featured
Glide API

The Power Trip: Mastering the Glide API

GlideRecord, GlideSystem, and GlideAggregate—these three classes are the holy trinity of server-side scripting in ServiceNow. Learn the essential methods and best practices for querying data, logging messages, and performing calculations on your instance.

CSA Prep Team
November 19, 2025
4 min read
Glide API
Best Practices
GlideRecord
GlideSystem
GlideAggregate
Business Rule

Why Glide Is the Core of ServiceNow

If you've spent time building Business Rules, Script Includes, or Scheduled Jobs, you've encountered the Glide API.

The Glide API is ServiceNow's robust, proprietary JavaScript library that allows you to interact directly with the platform's database and system functionalities from the server side. While low-code tools like Flow Designer handle basic automation, the Glide API is where you unlock complex integrations, deep data manipulation, and precise control over system behavior.

Mastering these three classes is the key to becoming a powerful ServiceNow developer:

  1. GlideRecord: Your primary tool for CRUD (Create, Read, Update, Delete) database operations.

  2. GlideSystem (gs): Your utility belt for system information, logging, and user feedback.

  3. GlideAggregate: Your calculator for statistical and summary data.

GlideRecord: The Database Operator

The GlideRecord class is your SQL equivalent in ServiceNow. It allows you to query, insert, update, and delete records in any table using object-oriented JavaScript methods.

Example 1: Querying and Printing Records

This common pattern finds all active P1 (Priority 1) incidents and logs their numbers.

// 1. Initialize the GlideRecord object for the Incident table
var grIncident = new GlideRecord('incident');

// 2. Build the query conditions (implicit AND condition)
grIncident.addQuery('active', true);
grIncident.addQuery('priority', 1);

// **Performance Tip:** Always limit your results for safety and speed!
grIncident.setLimit(5); 

// 3. Execute the query
grIncident.query();

// 4. Iterate and process the results
while (grIncident.next()) {
    // .number accesses the field value directly
    gs.info('High-Priority Incident Found: ' + grIncident.number);
}

Example 2: Creating a New Record

Use initialize() before setting field values and insert() to commit the record to the database.

var grNewTask = new GlideRecord('sc_task');
grNewTask.initialize();

grNewTask.short_description = 'Provision new user account via script';
grNewTask.assigned_to.setDisplayValue('System Administrator');
grNewTask.due_date = gs.daysAgo(-7); // Set due date to 7 days in the future

// Insert the new record and capture its sys_id
var newSysId = grNewTask.insert();

gs.info('New Task Created with Sys ID: ' + newSysId);

GlideSystem (gs): The System Utility Belt

The global object gs is an instance of the GlideSystem class. It is essential for getting information about the system, the current user, or communicating with the platform.

Example 3: User/Role Check and Messaging

This example, typically used in a Business Rule or Script Include, checks the user's role and provides immediate feedback.

// Check if the current user has the 'itil' role
if (gs.hasRole('itil')) {
    // Add an information message to be displayed at the top of the form
    gs.addInfoMessage('Welcome back, ITIL User!');

} else {
    // Log an error message to the system log (for developer debugging)
    gs.error('Access attempted by non-ITIL user: ' + gs.getUserName());
}

Example 4: Date/Time Calculations

GlideSystem makes complex date manipulation simple.

// Get the date string for the start of the current week (e.g., Sunday)
var startOfWeek = gs.beginningOfThisWeek();

// Log the result
gs.info('The current week started on: ' + startOfWeek);

GlideAggregate: The Data Summarizer

While GlideRecord retrieves records, GlideAggregate is used to get statistical data—like counts, sums, averages, minimums, and maximums—often achieving the result with fewer queries than GlideRecord.

Example 5: Counting Records by Category

This is the most efficient way to get a count of incidents grouped by category.

var gaIncident = new GlideAggregate('incident');

// 1. Specify the function (COUNT) and the field to group by
gaIncident.addAggregate('COUNT', 'category');

// 2. Specify the field to group the results by
gaIncident.groupBy('category');

// 3. Execute the query
gaIncident.query();

gs.info('--- Incident Counts by Category ---');

while (gaIncident.next()) {
    var categoryName = gaIncident.category.getDisplayValue();

    // Retrieve the aggregated COUNT value
    var count = gaIncident.getAggregate('COUNT', 'category');

    gs.info(categoryName + ': ' + count);
}

Final Best Practices for Glide

  1. Prioritize Low-Code First: Use Flow Designer or UI Policies before scripting. The Glide API is a last resort for complex server-side tasks.

  2. Always Use setLimit(): When querying with GlideRecord, always restrict the number of returned records to prevent performance degradation.

  3. Use GlideAggregate for Stats: Never use a GlideRecord loop just to count records; GlideAggregate is much faster.

Mastering the Glide API is a critical step in your journey toward becoming a top-tier ServiceNow developer, enabling you to build powerful, custom solutions that scale.

Ready to Master the CSA Exam?

Join our comprehensive study platform and get access to practice tests, study guides, and expert coaching.

Related Articles

Business Rules

ServiceNow Business Rules vs Data Policies: Complete Comparison Guide

Master the critical differences between Business Rules and Data Policies in ServiceNow. Learn when to use each, execution timing, practical examples, and best practices for CSA certification success.

13 min readRead more →
UI Policies

ServiceNow UI Policies vs Data Policies: When to Use Each

Understand the critical differences between UI Policies and Data Policies in ServiceNow. Learn when to use each type, their execution context, and best practices for CSA exam success.

10 min readRead more →
Integration

Make Client-Side API Calls for Catalog Item Population

Master the CSA-approved pattern for securely populating Service Catalog fields with external API data using Client Scripts, GlideAjax, Script Includes, and RESTMessageV2.

8 min readRead more →