ServiceNow Certification Mock Exams
Click To View Now
SNOWYCODE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
How To Auto Populate The Current User In ServiceNow
How To's

How To Auto Populate The Current User In ServiceNow

How To Auto Populate The Current User In ServiceNow

This may be one of the most common requirements for a ServiceNow developer. The story asks you to 'populate a field with the logged in user's name, email and other info'

There's a ton of use cases for populating data from the current user in ServiceNow, and it can make things very efficient for your client's Service Catalog.

ServiceNow makes this super easy to do - with just a single line of code placed directly on the variable itself. In most cases you won't even need to write a Client Script or Business Rule.

Let's go over how you can quickly populate user data in ServiceNow. We'll start with the actual code that you can copy/paste to achieve what you need, and then go a through a quick example.

If you're looking for the code snippet and don't need the tutorial, I've added the template code right below:

 // get users first name
javascript: gs.getUser().getFirstName();

// get users email
javascript: gs.getUser().getEmail();

// get users full name
javascript: gs.getUser().getDisplayName();

The Code - Setting The 'Default Value' In A Variable

The code calls gs.getUser which will return the user object. Once you call gs.getUser, you can call any of the built-in methods to retrieve the piece of user data you want.

For example, if you want to retrieve the user's first name and email, then you'd use the following code:

 // get users first name
javascript: gs.getUser().getFirstName();

// get users email
javascript: gs.getUser().getEmail();

// get users full name
javascript: gs.getUser().getDisplayName();

The code calls gs.getUser, and then calls two different built-in methods (getFirstName and getDisplayName).

For a full list of the methods you can call, refer to our User Object Cheat Sheet.

If ServiceNow doesn't have a built in method for the data you want, you can use .getRecord to pull any value off the user record. To do that, use the code below:

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

To auto-populate the field,  you can add the snippets from above to the 'Default Value' field on the variable and prefix it with 'javascript:' as shown below:

Default Value gets the current logged in user's name

Note: we have to prefix the code with 'javascript:' because it tells ServiceNow that we want it to read our text as JavaScript code. If we don't prefix it, then ServiceNow will assume our code is just text, and populate the default value with the literal text 'gs.getUser().getFirstName()' etc.

The variable on the catalog item will now look like this:

Default Value First Name With JavaScript Prefix

If you hadn't prefixed with 'javascript:' then your output would look like this:

Default Value First Name Without JavaScript Prefix

Depending on which value you need to populate, just swap out the methods or use .getRecord if there's no built-in method.

If you want to dive deeper into how gs.getUser works, we wrote a complete guide on using gs.getUser.

Quick Example

To see how this code works in a real implementation, we'll walk through a quick example and auto populate some fields on a Catalog Item.

Our catalog item has three fields: Name, Email, and Department. Since we already have this data in the user's record, we can make it easy for them and auto populate it on the form.

Catalog Item

To auto populate these fields, we'll use the following built in methods and place them inside of the 'Default Value' for each variable, prefixed with 'Javascript:'

 // get the users name (first and last)
gs.getUserDisplayName();

// get the users email address
gs.getUser().getEmail();

// get the users department
gs.getUser().getRecord().getDisplayValue('department');

Notice that name and email use built-in methods, while the department needs to be retrieved using .getRecord. This is because ServiceNow doesn't offer a built-in method for .getDepartment.

Below are images for what the 'Default Value' should look like for each field:

Note: as mentioned above, we have to prefix the code with 'javascript:' because it tells ServiceNow that we want it to read our text as JavaScript code. If we don't prefix it, then ServiceNow will assume our code is just text, and populate the default value with the literal text 'gs.getUser()' etc.

Default Value - Name Auto populated
Default Value for Name

Default Value - Email Auto populated
Default Value for Email

Default Value - Department Auto populated
Default value for Department

Now that we've set the default values for each field, next time we load the form it will look like this:

Fields Auto Populated On Catalog Item

As you can see, the Default Value auto populated the user's name, email and department into the form without the user's interaction.

On larger forms with several fields, this can be super helpful and save the user a lot of time.

In Summary

In this post, you've learned about the user object, how and why to prefix with 'javascript:' in the Default Value, and ultimately you've learned how to auto populate the current user in ServiceNow.

This is a superpower for any developer that is creating catalog items or record producers. Your requirements will often ask (and maybe even assume) that your user fields are auto populated.

Having a strong knowledge of auto populating user data is going to make a you better developer and allow you to finish your catalog items quicker.

We hope this article helped you gain that knowledge. Now go forth and conquer!

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