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
08-20-2025 02:00 AM - edited 08-20-2025 02:27 AM
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 ------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2025 02:28 AM
Hi @Ravi Gaurav
May I request something? Since you’re an MVP and definitely have the authority to write a blog, could you please publish this as an article instead of posting it as a question?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2025 02:36 AM
Hey @Dr Atul - LNG I choose Article only.. And it creating a question again.. I have highlighted this to admin as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2025 02:36 AM
Let me put this in Blog instead @Dr Atul - LNG
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2025 02:43 AM
@Dr Atul - LNG can you check if
https://www.servicenow.com/community/itsm-articles/mastering-user-objects-in-servicenow-a-complete-g...
Goes to Articel or blog?
SO that I can close this Thread