Blog

GlideRecord Query Methods

Glidesoft, Inc. incorporated in California, June 28, 2004.  Glidesoft, Inc. changed its name to Service-now.com on February 16, 2006. One of most powerful and useful javaclasses in ServiceNow is the GlideRecord. It is a special Java class (GlideRecord.java) that can be used in JavaScript exactly as if it was a native JavaScript class.

Ohh, that is why is is called Glide Record, ServiceNow used to be called GlideSoft!  Makes sense now.  What can you do with a Glide Record anyway?

You can use the GlideRecord API to: Query, Get, Set, Update, Insert, and Delete records in ServiceNow.  It is one of the most important things you can learn as a ServiceNow developer in my opinion. Let's check out some of different ways you can write a GlideRecord query.

In these examples below, I have seven incidents that are priority 5 with a category of inquiry.

If Statement

findIncident();
function findIncident() {
    var grIncident = new GlideRecord('incident');
    grIncident.addQuery('priority','5');
    grIncident.addQuery('category','inquiry');
    grIncident.query();
    if (grIncident.next()) {
        gs.print('Incident Found: '+grIncident.number);
    }
}

If you run this in Scripts - Background it will return one record, because a if statement is used to cycle through the query results.  This is good if you just want to find one record, however the query would have returned seven items, which isn't completely efficent.

SetLimit

findIncident();
function findIncident() {
    var grIncident = new GlideRecord('incident');
    grIncident.addQuery('priority','5');
    grIncident.addQuery('category','inquiry');
    grIncident.setLimit(1);
    grIncident.query();
    if (grIncident.next()) {
        gs.print('Incident Found: '+grIncident.number);
    }
}

If you run this in Scripts - Background it will return one record, because a if statement is used to cycle through the query results.  The setLimit statement helps performance, because only one record is returned with the query.
</li>

Encoded Query

findIncident();
function findIncident() {
    var grIncident = new GlideRecord('incident');
    grIncident.addEncodedQuery('priority=5^category=inquiry');
    grIncident.setLimit(1);
    grIncident.query();
    if (grIncident.next()) {
        gs.print('Incident Found: '+grIncident.number);
    }
}

 

If you run this in Scripts - Background it will return one record, because a if statement is used to cycle through the query results.  Using an encoded query is sometimes easier than multiple addQuery lines.  You can also use Copy Query to help figure out your encoded query content which is helpful.

While Statement

findIncident();
function findIncident() {
    var grIncident = new GlideRecord('incident');
    grIncident.addEncodedQuery('priority=5^category=inquiry');
    grIncident.query();
    while (grIncident.next()) {
        gs.print('Incident Found: '+grIncident.number);
    }
}

If you run this in <i>Scripts - Background</i> it will return seven records, because a while statement is used to cycle through the query results.  


Get Record

findIncident();
function findIncident() {
    var grIncident = new GlideRecord('incident');
    grIncident.get('priority','5');
    gs.print('Incident Found: '+grIncident.number);
}

If you run this in Scripts - Background it will return one record, because a get statement is used to cycle through the query results.  However, it can't run our full query of priority of 5 and category of inquiry.  This isn't one you would typically use in this situation.   It is the most useful only when used with the sys_id so you ensure you only get one record.   

Also check out this article: How to use Background Scripts

Mike