How To Get The Username Of Current User – User Object gs.getUser()

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. …

servicenow user object gs.getUser()

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())

MethodPurposeExample
getCompanyID()Returns the current user’s company sys_id.var currentUser = gs.getUser(); gs.print(currentUser.getCompanyID());
getUserName()Returns the user name of the current uservar currentUser = gs.getUser(); gs.print(currentUser.getUserName());
getDisplayName()Returns the current user’s display name.var currentUser = gs.getUser(); gs.print(currentUser.getDisplayName());
getDomainID()Returns the identifier of the user’s current session domain.var domain = new GlideRecord('domain'); domain.get(gs.getUser().getDomainID()); gs.print(domain.name);
getEmail()Returns the user’s email address.var currentUser = gs.getUser(); gs.print(currentUser.getEmail());
getFirstName()Returns the user’s first name.var currentUser = gs.getUser(); gs.print(currentUser.getFirstName());
getID();Gets the sys_id of the current user.var currentUser = gs.getUser(); gs.print(currentUser.getID());
getLastName()Returns the user’s last name.var currentUser = gs.getUser(); gs.print(currentUser.getLastName());
getName()Returns the user ID, or login name, of the current user.var currentUser = gs.getUser(); gs.print(currentUser.getName());
getPreference(String name)Gets the specified user preference value for the current user.var currentUser = gs.getUser(); currentUser.savePreference(­'myPref','blue'); gs.print(currentUser.getPreference(­'myPref'));
getRoles()Returns a list of roles that includes explicitly granted roles, inherited roles, and roles acquired by group membership.var currentUser = gs.getUser(); gs.print(currentUser.getRoles());
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.
var currentUser = gs.getUser(); gs.print(currentUser.getUserRoles());
hasRole(String role)Determines if the current user has the specified role.var currentUser = gs.getUser(); gs.print(currentUser.hasRole('admin'));
isMemberOf(String group)Determines if the current user is a member of the specified group.var currentUser = gs.getUser(); gs.print(currentUser.isMemberOf(­'Capacity Mgmt'));

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.



What Do You Think About This Article?

0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] How To Get The Username Of Current User – User Object gs.getUser() […]

1
0
Would love your thoughts, please comment.x
()
x