GlideSystem, commonly referred to as 'gs', is a powerful server-side class in ServiceNow that provides access to system-level information and utility methods. As a System Administrator or Developer, understanding gs methods is essential for scripting and automation tasks.
The gs object is availabl…GlideSystem, commonly referred to as 'gs', is a powerful server-side class in ServiceNow that provides access to system-level information and utility methods. As a System Administrator or Developer, understanding gs methods is essential for scripting and automation tasks.
The gs object is available in all server-side scripts, including Business Rules, Script Includes, Scheduled Jobs, and UI Actions. It offers numerous methods categorized into several functional areas.
**User and Session Methods:**
- gs.getUserID() - Returns the sys_id of the current user
- gs.getUserName() - Returns the username of the logged-in user
- gs.hasRole('role_name') - Checks if the current user has a specific role
- gs.getUser() - Returns a GlideUser object with detailed user information
**Logging and Debugging:**
- gs.log('message') - Writes to the system log
- gs.info('message') - Logs informational messages
- gs.warn('message') - Logs warning messages
- gs.error('message') - Logs error messages
- gs.debug('message') - Logs debug-level messages
**Date and Time Methods:**
- gs.now() - Returns current date and time
- gs.nowDateTime() - Returns current date-time in system format
- gs.daysAgo(days) - Returns a date from specified days in the past
- gs.beginningOfLastMonth() - Returns the start of the previous month
**System Properties and Information:**
- gs.getProperty('property_name') - Retrieves system property values
- gs.setProperty('name', 'value') - Sets system property values
- gs.tableExists('table_name') - Checks if a table exists
**Message and Event Methods:**
- gs.addInfoMessage('text') - Displays an info message to users
- gs.addErrorMessage('text') - Displays an error message
- gs.eventQueue('event_name', record, parm1, parm2) - Queues an event
These methods form the foundation of ServiceNow server-side scripting, enabling administrators to create dynamic, user-aware, and context-sensitive solutions.
GlideSystem (gs) Methods - Complete Guide
Why GlideSystem Methods Are Important
GlideSystem methods are fundamental to ServiceNow development and are essential knowledge for the CSA exam. These server-side methods provide developers with powerful tools to interact with the system, retrieve information, and perform common operations. Understanding gs methods demonstrates your ability to work with ServiceNow's scripting environment and is crucial for roles involving customization and automation.
What Are GlideSystem (gs) Methods?
GlideSystem, commonly referred to as gs, is a server-side JavaScript class that provides a collection of utility methods available in all server-side scripts. The gs object is globally accessible and offers functionality for:
• Logging and debugging • Retrieving user and session information • Working with dates and times • Displaying messages to users • Accessing system properties • Checking user roles and permissions
How GlideSystem Methods Work
GlideSystem methods are invoked using the gs prefix followed by the method name. Here are the most commonly used methods:
Logging Methods: • gs.info() - Writes an informational message to the system log • gs.warn() - Writes a warning message to the system log • gs.error() - Writes an error message to the system log • gs.debug() - Writes a debug message when debugging is enabled • gs.log() - Writes a message to the system log (legacy method)
User Information Methods: • gs.getUserID() - Returns the sys_id of the currently logged-in user • gs.getUserName() - Returns the username of the current user • gs.getUser() - Returns a GlideUser object for the current user • gs.getUserDisplayName() - Returns the display name of the current user
Session and Property Methods: • gs.getSession() - Returns the current session object • gs.getProperty() - Retrieves the value of a system property • gs.setProperty() - Sets the value of a system property
Message Methods: • gs.addInfoMessage() - Displays a blue informational message to the user • gs.addErrorMessage() - Displays a red error message to the user
Utility Methods: • gs.now() - Returns the current date and time • gs.nowDateTime() - Returns the current date and time in the system format • gs.nil() - Checks if a value is null or empty • gs.hasRole() - Checks if the current user has a specific role
Date Methods: • gs.daysAgo() - Returns a date that is a specified number of days ago • gs.beginningOfLastMonth() - Returns the start of the previous month • gs.endOfLastMonth() - Returns the end of the previous month
Exam Tips: Answering Questions on GlideSystem (gs) Methods
1. Know the Difference Between Logging Methods: Understand when to use gs.info(), gs.warn(), gs.error(), and gs.debug(). Questions often test your knowledge of appropriate logging levels.
2. Remember Server-Side Only: GlideSystem methods are exclusively server-side. If a question asks about client-side scripting, gs methods are not the correct answer.
3. User Information is Common: Expect questions about retrieving current user details. Know that gs.getUserID() returns a sys_id while gs.getUserName() returns the username.
4. Message Methods for User Communication: Remember that gs.addInfoMessage() shows blue banners and gs.addErrorMessage() shows red banners. These are used to communicate with users after form submissions.
5. Role Checking: gs.hasRole() is frequently tested. Know that it returns true or false based on whether the current user has the specified role.
6. Property Access: Understand gs.getProperty() retrieves system property values and is commonly used in scripts to make them configurable.
7. Null Checking: gs.nil() is the preferred method for checking if values are null or empty in ServiceNow. This is often tested in scenario-based questions.
8. Practice Common Scenarios: Review typical use cases such as logging debug information, checking user permissions before executing code, and displaying success or error messages after operations complete.