Mastering User Objects in ServiceNow – A Complete Guide with Real Examples
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hello ServiceNow Family,
When working with ServiceNow, one of the most common requirements is to fetch details about the currently logged-in user. Knowing who the user is, their roles, groups, and other attributes helps us deliver the right experience while ensuring proper access control.
ServiceNow provides two main ways to access user-related information:
-
GlideSystem (gs) User Object - Server-side
-
g_user Object - Client-side
This article explains both with real-world examples.
1. GlideSystem User Object (Server-side)
The GlideSystem user object (gs.getUser()
) is used in Business Rules, Script Includes, UI Actions, and other server-side scripts.
Common Methods & Examples
Method | Returns | Example |
---|---|---|
gs.getUser() |
User object of current user | var user = gs.getUser(); |
gs.getUserName() |
User ID (login name) | gs.info(gs.getUserName()); // 'employee' |
gs.getUserDisplayName() |
Full name of user | gs.info(gs.getUserDisplayName()); // 'Joe Employee' |
gs.getUserID() |
sys_id of user |
var id = gs.getUserID(); |
getFirstName() |
First name | gs.info(gs.getUser().getFirstName()); |
getLastName() |
Last name | gs.info(gs.getUser().getLastName()); |
getEmail() |
Email address | gs.info(gs.getUser().getEmail()); |
getMyGroups() |
Groups the user belongs to | gs.info(gs.getUser().getMyGroups()); |
isMemberOf(group) |
true/false if user is in a group |
if(gs.getUser().isMemberOf('Service Desk')) { ... } |
gs.hasRole(role) |
true/false if user has role |
if(gs.hasRole('itil')) {...} |
Example 1: Restricting Action Based on Role
if(!gs.hasRole('itil')){
gs.addErrorMessage("You must be an ITIL user to perform this action");
current.setAbortAction(true);
}
Example 2: Getting User’s Department
var dept = gs.getUser().getDepartmentID();
gs.info("Department Sys_id: " + dept);
Example 3: Fetch Any User Field
var title = gs.getUser().getRecord().getValue('title');
gs.info("User Title: " + title);
2. g_user Object (Client-side)
The g_user object is used in UI Policies and Client Scripts.
It is a cached copy of some user properties available on the client side.
Common Properties & Methods
Property/Method | Returns | Example |
---|---|---|
g_user.userName |
User ID | alert(g_user.userName); |
g_user.firstName |
First name | alert(g_user.firstName); |
g_user.lastName |
Last name | alert(g_user.lastName); |
g_user.userID |
Sys_id | alert(g_user.userID); |
g_user.hasRole('itil') |
true/false if user has role |
if(g_user.hasRole('itil')) {...} |
g_user.hasRoleExactly('itil') |
Checks role ignoring admin privilege | if(g_user.hasRoleExactly('itil')) {...} |
g_user.hasRoles('itil','admin') |
True if user has one of the roles | if(g_user.hasRoles('itil','admin')) {...} |
Example 4: Show/Hide Field for ITIL Users
if(g_user.hasRole('itil')){
g_form.setVisible('impact', true);
} else {
g_form.setVisible('impact', false);
}
Example 5: Check Group Membership (Client-side)
var grpName = 'Service Desk';
var usrID = g_user.userID;
var grp = new GlideRecord('sys_user_grmember');
grp.addQuery('group.name', grpName);
grp.addQuery('user', usrID);
grp.query(function(result){
if(result.next()){
alert('User belongs to Service Desk');
} else {
alert('User does not belong to Service Desk');
}
});
-------------- Thanks Everyone For Reading this article ------------------
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Ravi Gaurav
It is showing as an article & looks good.
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Thanks @Dr Atul G- LNG
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
No problem mate.
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************