SNOWYCODE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
User Object Cheat Sheet
Cheat Sheets

User Object Cheat Sheet

User Object (gs.getUser) Cheat Sheet for ServiceNow Devs

Below is a list of commonly used User Object code that you can come back to daily while writing scripts in ServiceNow.

The examples are grouped by:

  • gs.getUser() - GlideSystem
  • g_user - GlideUser

For a full summary on how to use gs.getUser, check out our complete guide.

GlideSystem User Object

Method Return Value Example
gs.getUser(); Returns a reference to the user object for the currently logged-in user var userObject = gs.getUser();
gs.getUserByID(); Returns a reference to the user object for the user ID (or sys_id) provided var userObject = gs.getUser().getUserByID('john_smith');
gs.getUserName(); Returns the User ID (user_name) for the currently logged-in user var userObject = gs.getUserName();
gs.getUserDisplayName(); Returns the display value for the currently logged-in user. i.e. 'John Smith' var userObject = gs.getUserDisplayName();
gs.getUserID(); Returns the sys_id string value for the currently logged-in user var userID = gs.getUserID();
gs.getFirstName(); Returns the first name of the currently logged-in user var firstName = gs.getUser().getFirstName();
gs.getLastName(); Returns the last name of the currently logged-in user var lastName = gs.getUser().getLastName();
gs.getEmail(); Returns the email address of the currently logged-in user var email = gs.getUser().getEmail();
gs.getDepartmentID(); Returns the department sys_id of the currently logged-in user var deptID = gs.getUser().getDepartmentID();
gs.getCompanyID(); Returns the company sys_id of the currently logged-in user var companyID = gs.getUser().getCompanyID();
gs.getCompanyRecord(); Returns the company GlideRecord of the currently logged-in user. var company = gs.getUser().getCompanyRecord();
gs.getLanguage(); Returns the language of the currently logged-in user var language = gs.getUser().getLanguage();
gs.getLocation(); Returns the location of the currently logged-in user var location = gs.getUser().getLocation();
gs.getDomainID(); Returns the domain sys_id of the currently logged-in user (only used for instances that are using domain separation) var DomainID = gs.getUser().getDomainID();
gs.getDomainDisplayValue(); Returns the domain display value of the currently logged-in user (only used for instances that are using domain separation) var domainName = gs.getUser().getDomainDisplayValue();
gs.getManagerID(); Returns the manager sys_id of the currently logged-in user var managerID = gs.getUser().getManagerID();
gs.getMyGroups(); Returns a list of all groups that the currently logged-in user is a member of var groups = gs.getUser().getMyGroups();
gs.isMemberOf(); Returns true if the user is a member of the given group, false otherwise var isMember = gs.getUser().isMemberOf();
gs.hasRole(); Returns true if the user has the given role, false otherwise if (gs.hasRole('snc_internal')) { do something }
gs.hasRole(); Returns true if the user has one of the given roles, false otherwise if (gs.hasRole('hr', 'itil')) { do something }
gs.hasRoles(); Returns true if the user has any roles at all, false if the user has no role (i.e. an ess user) if (gs.getUser.hasRoles() { do something }

In addition to the above methods, you can retrieve any information off of the user record by using the getRecord() method and passing in the field name. Below is an example of how to get the user's Department:

 gs.getUser().getRecord().getDisplayValue('department');

If you're looking for a way to auto populate the current logged in users details, check out our guide for auto populating user data.

g_user (GlideUser) Class

Method Return Value
g_user.userName Returns the Username of the current user (e.g. john_smith)
g_user.firstName Returns the First Name of the current user (e.g. John)
g_user.lastName Returns the Last name of the current user (e.g. Smith)
g_user.userID Returns the sys_id of the current user (e.g. 9d385027c611528721d22144cc95c572)
g_user.hasRole() Returns True if the current user has the role specified, false otherwise. Always returns true if the user has the 'admin' role.

Example: g_user.hasRole('catalog_admin')
g_user.hasRoleExactly(); Returns True if the current user has the exact role specified, false otherwise, regardless of 'admin' role.

Example: g_user.hasRoleExactly('knowledge_manager')
g_user.hasRoles(); True if the current user has at least one of the roles specified, false otherwise.

Example: g_user.hasRoles('itil', 'catalog_admin')

Looking for something else? Check out our other Cheat Sheets below!

Credit: ServiceNow Guru

Snowycode team
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Read more