A Comprehensive Guide to Glide Classes in ServiceNow Scripting.

A Comprehensive Guide to Glide Classes in ServiceNow Scripting.

ServiceNow scripting is a powerful tool for automating and customizing your IT service management processes. One of the core elements of scripting in ServiceNow is understanding the Glide classes, which provide access to various functionalities within the platform. In this article, we'll dive into some key Glide classes and their practical applications.

Server-side Glide Classes

GlideSystem

GlideSystem is your go-to class for managing user information and displaying messages to users. You can easily retrieve a user's sys_id with gs.getUserID() and access user details like full name, email, and more using gs.getUser().

To check if a user has a specific role, you can use gs.hasRole('admin'), allowing you to tailor script actions based on user roles. For logging purposes, use gs.log('Logging Message') to keep track of script activities.

GlideDateTime

GlideDateTime is essential for working with date and time fields. You can create a new GlideDateTime object and retrieve its display value or manipulate it by adding or subtracting time. For instance, glideDT.addDaysLocalTime(1) adds one day to the current date and time.

GlideElement

GlideElement facilitates working with fields in a GlideRecord object. You can trigger script actions when a field changes using current.state.changesTo('7'). Additionally, you can determine if a user can create, read, or write records, which is useful for UI action conditions.

GlideAggregate

GlideAggregate extends GlideRecord and is used for performing aggregate operations like counting records. You can use COUNT, SUM, MAX, MIN, and AVG to gather numerical insights from records.

Client-side Glide Classes

GlideForm

GlideForm is primarily for interacting with form fields. You can retrieve field values using g_form.getValue('state'), set fields as mandatory, read-only, or hidden, and even check if a record is new with g_form.isNewRecord().

GlideUser

GlideUser is useful for accessing user-related information. You can retrieve the logged-in user's sys_id and full name. It also allows you to check if a user has a specific role using g_user.hasRole('itil').

Script Examples

Now, let's explore some practical script examples to illustrate the power of Glide classes.

Using GlideRecord

Here, we use GlideRecord to create problem records for critical priority incidents. We loop through incident records, create corresponding problem records, and link them together.

var incRec = new GlideRecord('incident');
incRec.addQuery('priority', 1);
incRec.query();
while (incRec.next()) {
    // Create a new problem record
    var newProblemRec = new GlideRecord('problem');
    newProblemRec.cmdb_ci = incRec.cmdb_ci;
    newProblemRec.short_description = incRec.short_description;
    var newInsertedRecord = newProblemRec.insert();
    // Update the incident with the problem reference
    incRec.problem_id = newInsertedRecord;
    incRec.update();
}

        


Article content

Setting Field Values Conditionally

You can use scripts to set field values conditionally based on whether a record is new or not. For example, setting a category field to "inquiry" for new incident records:

if (g_form.isNewRecord()) {
    g_form.setValue('category', 'inquiry');
}

        

In Conclusion

Understanding and harnessing the power of Glide classes in ServiceNow scripting is essential for effective process automation and customization. Whether you're managing user data, manipulating date and time, or working with form fields, these classes offer the tools you need to streamline your ServiceNow experience. By combining them with practical script examples, you can take your ServiceNow scripting skills to the next level and optimize your IT service management processes.

Author: Ali Subhan

LinkedIn: https://www.garudax.id/in/ali-subhan07/


To view or add a comment, sign in

More articles by Ali Subhan

  • Mastering 80% ServiceNow with Glide APIs

    Diving into ServiceNow’s Glide APIs has been an incredible journey. These APIs are essential for efficient data…

Others also viewed

Explore content categories