When you are scripting around the current user, you’ll find that the user object comes in handy.
There are often times you’ll need to perform some certain behavior around the current user who’s logged in.
Not all users are created equally in ServiceNow.
As a ServiceNow Developer, you’ll find that having access to the user object will reduce the number of sys_user queries that you need to run and will save you a lot of time. This applies to the current logged in user. You already have the data at your disposal, so you may as well use it.
You can run all of the below examples in a background script in ServiceNow, which will use your own user account as the current logged in user.
Once you have access to the user object, you’ll be able to find things like their:
- First Name
- Last Name
- Email Address
- Manager’s Name
- User Name
- Time Zone
- All Roles
- Group Membership
- Company name
You can access the user object on the server side, so in a background script, business rule or script include.
For example, to get the username of the current user, you can use the below script:
var user = gs.getUser(); var userName = user.getUserName(); // this will be the user name of the current user
Give the following examples a try in a background script in the development environment to get used to how this all works.
The User Object – gs.getUser()
The user object is a part of the GlideUser API in ServiceNow.
The user object allows you to gain access about the current user.
This will remove the need to run GlideRecord queries on the user table, and gives you immediate access, that you can simply store in a variable.
This can be run in any server side operation in ServiceNow.
Below are all of the examples that we’re aware of for the user object.
You simply need to gain access to the object, and then store the value of whatever method you’re looking for.
You do this by getting the object (gs.getUser()), and then accessing the method off of the object (gs.getUser().chooseMethod())
|getCompanyID()||Returns the current user’s company sys_id.|
|getUserName()||Returns the user name of the current user|
|getDisplayName()||Returns the current user’s display name.|
|getDomainID()||Returns the identifier of the user’s current session domain.|
|getEmail()||Returns the user’s email address.|
|getFirstName()||Returns the user’s first name.|
|getID();||Gets the sys_id of the current user.|
|getLastName()||Returns the user’s last name.|
|getName()||Returns the user ID, or login name, of the current user.|
|getPreference(String name)||Gets the specified user preference value for the current user.|
|getRoles()||Returns a list of roles that includes explicitly granted roles, inherited roles, and roles acquired by group membership.|
|getUserRoles()||Returns the list of roles explicitly granted to the user.|
Unlike the getRoles() method, this method does not return roles the user inherits or roles acquired from group membership.
|hasRole(String role)||Determines if the current user has the specified role.|
|isMemberOf(String group)||Determines if the current user is a member of the specified group.|
Let us know what you think below.
There may be some methods that we’ve left out or forgotten about, so let us know if you know of any.