4

ServiceNow Server-Side Scripting Basics and Glide APIs

In Module 3, we learned client-side scripting, which controls what users see and do in the browser. But you can’t trust the browser with everything. Data rules, record updates, and integrations need to run where the data lives, the server.

In this chapter, we will cover server-side scripting in ServiceNow: what it is, where to use it, and the server-side Glide classes and APIs you will use most often.

What is server-side scripting in ServiceNow?

Server-side scripting in ServiceNow means writing JavaScript code that runs on the ServiceNow server rather than in the user’s browser. It is mainly used to handle business logic, automate tasks, manage database operations, and maintain data security.

It allows developers to control how data is processed and how the system behaves behind the scenes.

Some common examples include:

  • Updating a field automatically when a new record is created.
  • Updating a record when its parent record changes.
  • Checking the role of the currently logged-in user.
  • Generating events or sending email notifications automatically.

Where Can You Use Server-Side Scripting?

ServiceNow provides several platform features where developers can write and execute server-side scripts. Here are some commonly used places:

Platform Feature Purpose
Business Rules Run scripts when a record is displayed, inserted, updated, deleted, or queried.
Script Includes Store reusable JavaScript code that can be called by other scripts.
Access Control Lists (ACLs) Control who can view, create, update, or delete records.
Transform Maps Transform and process data while importing it into ServiceNow tables.
Workflow and Flow Designer Execute scripts as part of automated workflows and flows.

If you only need to change how a form looks or behaves, a UI policy or client script is the better choice. To write any of these scripts, you need ServiceNow’s ready-made classes. Let us look at them next.

Server side Glide class

Server side Glide classes in ServiceNow are JavaScript APIs that execute on the server to interact with the database, system settings, business logic, and records without writing SQL. Glide classes expose JavaScript APIs that enable you to work with tables using scripts. It lists five core server-side Glide classes:

Server-Side Glide Class What It Does
GlideRecord Handles database operations on a table.
GlideSystem (gs) Gives information about the system and the logged-in user.
GlideAggregate Runs COUNT, SUM, MIN, MAX, and AVG queries.
GlideDateTime Handles date and time calculations and formatting.
GlideElement Helps you work with individual fields within a ServiceNow record.

Other classes, such as GlideFilter, GlideSysAttachment, and RESTMessageV2, cover more specific needs like filters, attachments, and web service calls.

Server-Side APIs

Server-side APIs in ServiceNow, commonly known as ServiceNow Glide APIs, are built-in JavaScript classes and methods that execute on the ServiceNow server rather than in the user’s browser.

Developers use these APIs to perform database operations, apply business logic, automate workflows, retrieve system information, and manage platform integrations.

Common Server-Side APIs

ServiceNow provides a comprehensive set of server-side classes. Some of the most frequently used APIs include: 

GlideRecord is the primary API to work with the database in server-side code. It allows you to create the object, add conditions, run the query, and loop through the records in tables.

GlideSystem is available in server-side scripts as gs. It gives you system and user information, and it is also used to log messages. It provides methods for interacting with the system, such as retrieving user details, logging messages, triggering events, and checking system properties. 

Method What It Does
gs.info() Writes a message to the log.
gs.addInfoMessage() Shows an info message to the user.
gs.addErrorMessage() Shows an error message to the user.
gs.getUserID() Returns the sys_id of the current user.
gs.hasRole(‘role’) Checks if the user has a role.
gs.eventQueue() Used to trigger the event.

GlideAggregate is a server-side ServiceNow API that performs calculations like COUNT, SUM, AVG, MIN, and MAX on database records. Instead of fetching and looping through records, it performs calculations directly in the database, making queries faster and more efficient.

Example: 

var ga = new GlideAggregate('incident');

ga.addQuery('active', true);

ga.addAggregate('COUNT');

ga.query();

if (ga.next()) {

    gs.info('Active incidents: ' + ga.getAggregate('COUNT'));

}

GlideDateTime is a built-in server-side ServiceNow API used to create, format, compare, and modify date and time values. It also supports timezone conversions and duration calculations.

Example:

var gr = new GlideRecord('incident');

if (gr.get('number', 'INC0402714')) {

    // Retrieve the incident creation date and time

    var createdOn = new GlideDateTime(gr.getValue('sys_created_on'));

    // Display the date and time in the user's time zone

    gs.info("Created On: " + createdOn.getDisplayValue());

    // Modify the date by adding 2 days

    var updatedDate = new GlideDateTime(createdOn);

    updatedDate.addDays(2);

    gs.info("Date After 2 Days: " + updatedDate.getDisplayValue());

    // Compare the original and modified dates

    if (updatedDate.after(createdOn)) {

        gs.info("The modified date is later than the original date.");

    }

    // Format the date and time

    gs.info("Formatted Date: " + createdOn.getByFormat('dd-MM-yyyy HH:mm:ss'));

};

}

What’s Next

Sometimes a server-side script needs to read, create, update, or delete records in a table. GlideRecord handles this. In the next topic, we will understand GlideRecord in ServiceNow through a complete scripting guide.

Next Topic

Need more support?

Get a head start with our FREE study notes!

Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.

Book Free15-Minutes Career Counselling