In 2003, CEO Fred Luddy started a company called GlideSoft, which specialized in IT Service Management applications. Three years later, the company changed its name to ServiceNow, but the Glide name stuck for the API!
The Glide API is available on both the server and the client (though only a subset of the API is available on the client), and it consists largely of several classes about which we'll learn in this chapter; several of which are:
Each class in the Glide API may consist of methods (executable functions within the class), and properties (variables stored within the class, which may be set on initialization of a new instance of the class).
This chapter largely consists of API documentation. We recommend that you read through it once, and then dog-ear this chapter so that you can come back to it for reference later on.
First, let's define a few terms. According to Mozilla's object-oriented JavaScript documentation, a class is sort of like a template for an object. It pre-defines the methods and properties of an object generated from the class.
An object therefore, is simply an instance of a class!
A method is a function or subroutine that's declared as part of a class (and any objects that are instances of that class).
A constructor is a special type of function that runs when an object is declared as a new instance of a class using the new keyword. In ServiceNow, this constructor function is usually called initialize. Any arguments passed into the class during instantiation will be passed into this function, which effectively builds the object that will be returned.
Naming convention dictates that while variable names use camelCase capitalization, class names use SentenceCase capitalization.
Here is a simple example of what a very simple class might look like:
var MyClass = Class.create();
MyClass.prototype = {
initialize: function(initArg) {
this.localProperty = initArg;
},
getLocalProperty: function() {
return this.localProperty;
},
type: 'MyClass'
};
Now let's break this down line-by-line, and see how it works.
On line 1, we declare our class (MyClass) using a special ServiceNow API, Class.create(). As it happens, JavaScript doesn't technically have a data type called Class. Instead, it relies on function/object prototype extensions. In order to simplify this, and provide syntax more similar to the backend Java (and more similar to what most object-oriented programming languages use), ServiceNow provides Class.create() as a simple way to generate a basic class-esque object.
On line 2, we take this basic class scaffolding that we generated on line one, and extend that prototype by adding some stuff to it (everything between the curly braces: { and }).
On line 3, we declare the initialize method. This is a special method of our class, which is called automatically whenever a new object is generated (instantiated) from our class using the new keyword like so:
var myObj = new MyClass('input');
Any arguments passed into this instantiation process (the string input in the preceding case), are passed into this initialize method.
Next, on line 4, we set a property in the scope of the instantiated object using the keyword this. In this case, any time we create an instance of this class, the property called localProperty will be initialized with the value that was passed into the constructor function.
On line 6, we declare another method called getLocalProperty. This is a method that can be called from objects created from our class, and which (on line 7) returns the value of the property that was set on initialization.
Finally, on line 9, we declare the type property of our class (and instantiated objects). This is just a string that you can access to determine what class child objects were created from.
Server-side APIs consist of classes and methods that are available to scripts executing on the server. When a script executes on the server, it is able to do so because of an open-source implementation of JavaScript, written in the Java programming language, called Mozilla Rhino. This allows us to interact with Java applications such as ServiceNow by scripting in JavaScript, despite the fact that the languages are not inherently cross-compatible.
The GlideRecord class is one of the most ubiquitous and useful classes in ServiceNow. Its primary function is to query a database table, and present values corresponding to each record in that table, that matches a given query. It can also be used to add, modify, or delete records. A GlideRecord object consists of properties with names corresponding to each field in the table. In the client-side Glide API, these properties usually contain strings, whereas on the server-side API, these properties contain GlideElement JavaScript Objects with their own methods and properties.
A GlideRecord object must first be initialized by using the new keyword (which calls the initialize() constructor method) and passing in a table name as a string. This tells the new GlideRecord object what table any subsequent queries or new records are created on.
Initialize a new GlideRecord object on the Incident table, and store it in the gr variable:
var gr = new GlideRecord('incident');
The addQuery() method of the GlideRecord class can be called in three different ways, depending on whether one, two, or three arguments are supplied to it. If one argument is passed into the addQuery() method, then it'll assume that the argument is an encoded query. Encoded queries are a single string that represents all of the conditions in a single query (or even multiple queries)!
There isn't any official documentation on how to construct an encoded query (though it isn't hard to figure out), but there is a very easy to build one yourself - simply navigate to the table you want to run the query on, and use the query builder! For example, here's a query on the incident table where the "assigned to" user is active, and either the Incident is active, or the state is set to new, in progress, or on hold:
You can build a filter on a given table using the query builder, and then apply that same filter to your GlideRecord query by simply right-clicking the last condition in the query breadcrumbs above the list view, and clicking on Copy query.
This is similar to the legacy addEncodedQuery() method:
You'll see an example of this single-argument usage in the following section. On the other hand, if you pass in two arguments, then it is assumed that the first argument is the field name, and the second argument is the expected value.
Finally, if you pass in three arguments to the addQuery() method, you may specify not only a field and value, but an operator between them. Operators that you can...