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.
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:
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:
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.
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.
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.
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.
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.
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 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!