Chapter 9. The Server-Side Glide API

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, which is why we have APIs like GlideSystem (gs) and GlideRecord. Since then, ServiceNow has renamed its namesake platform from the ServiceNow platform to the Now platform, though the company name remains ServiceNow.

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 of several classes which we'll learn about in this chapter; including:

  • Server-side Glide classes:
    • GlideRecord
    • GlideAggregate
    • GlideElement
    • GlideDate and GlideDateTime
    • GlideSession
    • GlideSystem
  • Client-side Glide classes:
    • GlideRecord
    • GlideAjax
    • GlideForm
    • GlideList2
    • GlideMenu
    • GlideUser

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).

Note

This chapter largely consists of details about most of the APIs you're likely to commonly use in ServiceNow development. 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.

The structure of an API class

First, let's define a few terms. According to Mozilla's object-oriented JavaScript documentation, JavaScript is really a classless language. However, class syntax was added on top of the language, so in JavaScript, a class is sort of like a template for an object. A class is technically an object as well. A class definition pre-defines the methods and properties of an object generated from the class. Most experienced JavaScript developers will be familiar with a similar concept: object prototypes.

An object therefore, in this context, is simply an instance of a class! These classes are only slightly different from the prototype constructors you're probably familiar with, and instances of these classes are generated in the same way as with prototypal notation: using the new keyword.

var grIncident = new GlideRecord('incident');

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's server-side scripts (script includes, specifically), this constructor function is usually called initialize. Any arguments passed into the class during instantiation (using the new keyword) will be passed into this function, which builds the object that will be returned.

Note

Naming convention dictates that while variable names (including function names) use camelCase capitalization, class names and stand-alone constructor function names use SentenceCase capitalization.

Here's an 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 and give it a name (MyClass) using a special ServiceNow API: Class.create(). JavaScript doesn't technically have a data type called Class; at least not in ES5, which is the version of JavaScript that executes on the server. Instead, this code 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 and onward, 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 (initArg).

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.82.23