OBLYTECH Logo
OBLYTECH Logo

gs.getSession() in ServiceNow server scripts

gs.getSession() returns the session object for the user running the current server script. This page covers that one call and, because it is the single entry point into the GlideSession class, also covers the methods on the object it returns: language, time zone, login state, interactive mode, roles, impersonation, domain and client data.

What gs.getSession() returns

The global GlideSystem method takes no parameters. It returns a GlideSession object for the current session, so you call it once, store the result in a variable and call methods on that variable.

var session = gs.getSession();
var language = session.getLanguage();
var zone = session.getTimeZoneName();

In scoped applications the same entry point exists on the scoped GlideSystem API. The scoped GlideSession class has no constructor, so the getSession() call on the scoped system object is the way to obtain one.

The scoped class exposes more methods than the global class, including impersonation checks and domain lookups. It does not carry over two global methods: getRoles() and clearClientData() are absent from the scoped list, so scoped code checks roles with hasRole() on the user object instead.

Methods on the GlideSession class

The global GlideSession class, as listed in the Brazil release, has eight methods. The table groups them by what they tell you.

Method Parameters Returns What it gives you
getLanguage() none String The session’s language code
getTimeZoneName() none String The name of the session’s time zone
isLoggedIn() none Boolean Whether the current user is logged in
isInteractive() none Boolean Whether the session has an end-user interface
getRoles() none String A comma-separated list of the user’s roles
putClientData() name, value void Stores a value that client scripts can later read
getClientData() name String Reads a value stored with putClientData()
clearClientData() name void Takes a parameter name and returns nothing

The scoped GlideSession list adds six methods: getClientIP(), getCurrentDomainID(), getCurrentApplicationId(), getSessionToken(), getUrlOnStack() and isImpersonating(). They are covered in the sections below.

Language and time zone for formatting

Two common reasons to read the session are formatting and localisation. getLanguage() returns the code for the session’s language, and getTimeZoneName() returns the name of the session’s time zone. A script that builds a date string for a notification can read the time zone from the session instead of hard coding one, so the output lines up with what the user sees in the interface.

These values describe the session, not the record. A script that formats a date for a different audience from the person running it has to supply its own language and time zone.

Checking whether the user is logged in

isLoggedIn() returns a Boolean that is true when the current user is logged in. Placing it ahead of logic that depends on a user identity, such as an approval step or a personal filter, makes the dependency explicit at the top of the script.

Interactive and programmatic sessions

isInteractive() tells a script whether the session involves an end-user interface. The method contrasts an end-user interface with programmatic access, and a SOAP request is one example of a non-interactive call. Two details matter in practice. isInteractive() returns false for user sessions that use the classic or current mobile applications, so a script that expects a browser-style session has to allow for that result.

Roles can be stale inside a session

getRoles() returns a comma-separated string of the current user’s roles. The list does not reflect changes made during the current user session. If an administrator adds or removes a role while the user is signed in, getRoles() keeps returning the old list until the user logs out and back in.

For role-sensitive checks, use hasRole() on the user object rather than getRoles(), particularly where role masking or role delegation is involved. hasRole() answers the question a script usually needs: does this user hold this role right now. getUserRoles() is also available on the user object, but it returns just the explicitly granted roles and leaves out inherited and group-acquired ones.

The practical rule is to call hasRole() for access decisions and to treat getRoles() as a display list rather than a security input.

Domain and application context

Scoped scripts can read two context values that matter in multi-domain or multi-application setups.

getCurrentDomainID() returns the sys_id of the user’s current session domain, with three cases to know. A user in the global domain who does not use the domain picker gets null. A user who switches to the global domain with the picker gets the string “global”. Any other domain returns that domain’s sys_id. One pattern copies this value into a record’s sys_domain field when the record’s domain is empty or global, which stamps new records with the domain they were created in.

getCurrentApplicationId() returns the application selected in the application picker. It requires admin privileges, so a script running as a non-admin user cannot rely on its result. Keep it to administration and tooling logic rather than ordinary business rules.

Impersonation

isImpersonating() returns true when the current user is acting as another user. Run this check before any action that should be attributed to the real person, such as an audit entry or an approval. Because it returns a Boolean, it reads cleanly in a conditional:

if (gs.getSession().isImpersonating()) {
  gs.info('Action taken while impersonating another user');
}

Client IP, URL stack and session token

getClientIP() returns the client IP address for the session. It is useful for logging and for simple network-based rules. It is usually one signal among several, not proof of who is making the request.

getUrlOnStack() returns the URL on the stack and returns null when the stack is empty. Code that depends on it needs to handle the null case.

getSessionToken() returns the session token. On the scoped GlideSystem object the token method is no longer offered, so code that called it there should call gs.getSession().getSessionToken() instead.

Passing values from server to client

putClientData() stores a named value on the session, and getClientData() returns that value. The client side of that exchange is covered in client script basics.

clearClientData() takes a parameter name and returns nothing. It is not on the scoped GlideSession list.

Identity methods used alongside the session

The GlideSession class has no method for the user’s login name, in either its global or scoped list. Identity values come from the GlideSystem object and the GlideUser object instead.

gs.getUserID() returns the sys_id of the current user. gs.getUserName() returns the login name, for example abel.tuter, not the display name. gs.getUserDisplayName() returns the display name. gs.getUser() returns a reference to the user object for the current user, and in scoped code it returns a GlideUser.

On the user object, getID() returns the sys_id, getName() returns the login name and getDisplayName() returns the display name. getEmail(), getFirstName() and getLastName() return the matching profile fields.

gs.getProperty(key, alt) is not a session method, but it is used in the same kinds of scripts. It returns the value of a system property, or the alternate value you supply if the property does not exist.

Choosing between session and user methods

Session methods describe the context of the current run: the language, the time zone, whether the run is interactive, whether the user is impersonating someone, and which domain applies. User methods describe the person: the sys_id, login name, display name, email and roles. A script that records who acted should read identity from the user object and context from the session, which keeps each value in the object that owns it.

Choose the call by the question the script is answering. If the question is “who is this?”, start with gs.getUserID() or gs.getUser(). If it is “what situation is this running in?”, start with gs.getSession(). If it is “is this user allowed to do this now?”, use hasRole() on the user object, because it reflects the current state of the user’s roles rather than a list captured earlier in the session.

Frequently asked questions

Does gs.getSession() take any parameters?

No. The global GlideSystem method takes no parameters and returns a GlideSession object for the current session. Call it with empty parentheses, then call methods on the result.

Why does getRoles() still show a role I removed?

The roles list does not reflect changes made during the current user session. The user needs to log out and log back in before getRoles() shows the update. For a check that must respond to role changes straight away, use hasRole() on the user object.

How can a script tell whether it is running in an interactive session?

Call isInteractive() on the session. It returns false for programmatic access such as a SOAP request, and it also returns false for user sessions on the classic and current mobile applications.

How do I get the login name of the current user?

Use gs.getUserName(), which returns the login name. The session class has no method for it. The display name comes from gs.getUserDisplayName().

How do I check whether someone is impersonating another user?

Call isImpersonating() on the scoped session object. It returns true when the current user is acting as another user, which makes it the right check before recording an action against the real person.

Who can call getCurrentApplicationId()?

The method requires admin privileges. A script running without admin rights should not depend on its result, so keep the call to administration and tooling logic.