ServiceNow Certification Mock Exams
Click To View Now
SNOWYCODE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Using gs.getUser in ServiceNow - The Complete Guide
How To's

Using gs.getUser in ServiceNow - The Complete Guide

Using gs.getUser in ServiceNow - The Complete Guide

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.

What Data Can gs.getUser Access?

Here are some (not all) of the things the user object has access to:

  • First Name - getFirstName();
  • Last Name - getLastName();
  • User Name  - getUserName();
  • Email Address - getEmail();
  • Location - getLocation();
  • Group membership - isMemberOf('Group Name');

For a full list of gs.getUser methods, check out our full ServiceNow User Object Cheat Sheet.

Where Can I Use gs.getUser?

The user object is server side, so you can access it in any server side operation, including the following:


Examples Using The User Object

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.

Syntax - two different ways to use gs.getUser

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:

 var userObject = gs.getUser();

var firstName = userObject.getFirstName(); // returns the first name of the currently logged in user

var email = userObject.getEmail(); // returns the email of the currently logged in user

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:

 var firstName = gs.getUser().getFirstName();

Notice how the user object itself is never stored into its own variable, you simply call getFirstName in the same line.

Getting the user's email address

If you'd like to retrieve the email address of the currently logged in user, you could do the following:

 var currentUser = gs.getUser();
var
usersEmail = currentUser.getEmail(); // this is the current users email gs.info('The current users email is ' + usersEmail);

Checking the user's roles

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:

 answer = true;
   if(gs.hasRole('major_incident_manager'))
	answer = true;
   else {
	answer = false;
 }

A shorter way to write this would be:

 answer = gs.hasRole('major_incident_manager');

Checking the user's groups

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.

 answer = false;
   if(gs.getUser().isMemberOf('Demand Management'))
	answer = true;
   else {
	answer = false;
 }

A shorter way to write this would be:

 answer = gs.getUser().isMemberOf('Demand Management');

Find all of the current user's groups

If you'd like to find all groups that the current user is a member of, you could do the following:

 var currentUser = gs.getUser();
var
usersGroups = currentUser.getMyGroups(); // returns comma separated list of sys_id's of the user's groups

Find the current user's company

Use getCompanyID to return the sys_id of the current user's company. Let's look at an example:

 var company = gs.getUser().getCompanyID; // returns sys_id of the current user's company

Find the current user's ID

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:

 var userID = gs.getUser().getID();

Find the current user's display name

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:

 var userName = gs.getUser().getName();

Summary

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.

Looking for something else? Check out our other posts below!

Interested in making more money as a ServiceNow developer? We're here to help. We're launching a set of courses, 1:1 mentorship and a community to help you increase your earnings.

To learn more, sign up here: Make More Money As A ServiceNow Developer

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

Read more