What It Does
The getSession() method retrieves the current user's session object, which contains contextual information about their active ServiceNow session. The session object persists throughout the user's interaction with the platform and provides access to timezone settings, language preferences, client IP address, and other session-specific data that may not be directly available through other APIs.
Internally, ServiceNow maintains session state in memory and ties it to the user's authentication token or session cookie. When you call gs.getSession(), the platform looks up the current execution context and returns the associated GlideSession object. This works reliably in user-initiated contexts like Business Rules triggered by form submissions, but behaves differently in system-initiated contexts like scheduled jobs.
The method always returns a GlideSession object, never null. However, the session object's properties may contain different values depending on the execution context. In scheduled jobs or system processes, the session represents the system context rather than a specific user's session, which means timezone and language settings default to system values rather than user preferences.
Edge cases occur when the execution context changes mid-process. For example, if a Business Rule calls a Script Include that performs an impersonation using gs.getUser().impersonate(), the session object continues to reflect the original user's session data, not the impersonated user's preferences. The session is bound to the HTTP request context, not the current user context.
This method differs from gs.getUser() in that it focuses on session-level data rather than user profile data. While getUser() returns user record information and roles, getSession() returns contextual information about how the user is currently interacting with the platform.
When to Use This
Use getSession() when you need to access timezone information for date formatting, determine the user's language for internationalization, or retrieve client-specific data like IP address for logging or security purposes. This method is particularly valuable in Business Rules that need to format dates or times according to the user's timezone preferences, or in Script Includes that generate localized content.
Avoid using this method when you need user profile information like roles, departments, or contact details—use gs.getUser() instead. Don't use session data for security decisions since session properties can be manipulated on the client side. Session data should inform presentation and formatting decisions, not access control or business logic validation.
Common misuse includes storing sensitive data in session variables or assuming session data persists across different types of script execution. Session variables set in one Business Rule may not be available in asynchronous processes or scheduled jobs that run later.
Return Value
The method returns a GlideSession object that provides methods like getTimeZoneName(), getLanguage(), getClientIP(), and property access methods. The object is always instantiated, so you don't need null checks on the session object itself, but individual property values may be empty strings or default values depending on the context.
The returned object is live and connected to the current session state. Changes made to session properties through the GlideSession object's methods will persist for the duration of that user's session, making it useful for maintaining state across multiple requests within the same session.
Enjoying this? Get one deep-dive per week.
Join 1,000+ ServiceNow pros — scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.
Platform Behavior & Side Effects
- No Business Rules, notifications, or audit records are generated by simply calling this method—it's a read-only operation
- No database writes occur unless you modify session properties through the returned
GlideSessionobject's setter methods - Performance is fast—session objects are cached in memory and retrieved without database queries
- Behavior is consistent across before/after Business Rules and Script Includes when called within the same HTTP request
- In scheduled jobs and system processes, returns a system session object with default timezone and language settings rather than user-specific values
- Session modifications may not be immediately visible in client-side scripts since they operate in a different execution context
Session data persists only for the duration of the user's active session and should never be relied upon for permanent data storage or cross-session state management.