The user object is a very helpful way to pull data from a user in a business rule or other server script. It allows you to decrease the amount of GlideRecord queries you need to run on sys_user, making it a much more efficient method of retrieving user data in ServiceNow.
Here are some (not all) of the things the user object has access to:
For a full list of gs.getUser methods, check out our full ServiceNow User Object Cheat Sheet.
The user object is server side, so you can access it in any server side operation, including the following:
gs.getUser is most commonly used to fetch data for the currently logged in user. However, you can fetch data for any user by using gs.getUserByID and passing in the target user's sys_id.
Let's go through some examples of how gs.getUser is used in ServiceNow.
First things first, let's discuss the syntax or a gs.getUser call. Our examples will use both, so that we can show each way of doing it.
The first way is to store the user object in a variable, and then call your methods from that. For example, if we wanted to store the user object, then pull the first name and email off of it, we'd do this:
The second way is quicker. It involves using gs.getUser to call your methods all in one line.
If you only need to pull a single piece of data from the user object in a short block of code, this may be the more optimal method.
For example, if you want the user's first name, you do this:
Notice how the user object itself is never stored into its own variable, you simply call getFirstName in the same line.
If you'd like to retrieve the email address of the currently logged in user, you could do the following:
A common use of gs.getUser is to determine if the current user has a specified role. For example, you may need to check the user's role as part of an ACL script to verify that they were explicitly granted roles. This is where the hasRole method comes in.
You pass in the name of the role, and the method returns a value of true or false depending on if the user has the specified role or not.
For example, if you wanted to verify that the user was a member of the 'major_incident_manager' group in an ACL script, you'd write the following:
A shorter way to write this would be:
A similar use case involves the isMemberOf method. It does essentially the same thing as hasRole, but it is used to check if the user is a member of a specified group.
The method will check the group members, and return a value of true if the current logged in user is a member. If the currently logged in user is not a member of the group you passed in, it will return a value of false.
Let's use the ACL example again, but for isMemberOf this time.
A shorter way to write this would be:
If you'd like to find all groups that the current user is a member of, you could do the following:
Use getCompanyID to return the sys_id of the current user's company. Let's look at an example:
It can always be useful to find the sys_id of the current user. In order to do that, we use getID as shown below:
We have .getFirstName and .getLastName to retrieve a user's actual name, but sometimes we want the user's username. This is where .getName comes in.
By calling .getName, we can return the value of the user's ID:
Having a basic understanding of the user object and how to access it with gs.getUser can be a huge advantage when you're developing daily. Taking some time to learn and understand it can pay dividends the rest of your ServiceNow career.
The official ServiceNow docs are also a great source of information on accessing the user object and following best practices.
If you have any questions or feedback on the article, reach out to us at servicenowresource@gmail.com.