Mastering User Objects in ServiceNow – A Complete Guide with Real Examples
New article articles in ServiceNow Community
·
Aug 20, 2025
·
article
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')) {...} |
3. When to Use What?
- Use GlideSystem (gs.getUser()) → When working server-side (Business Rules, Script Includes, etc.).
- Use g_user → When working client-side (UI Policies, Client Scripts) and you need quick access to common properties.
For additional attributes not available in g_user, you must run a GlideRecord query on sys_user.
4. Real-world Scenarios
Scenario A: Auto-populate Caller’s Department in Incident
Business Rule Script (Server-side):
current.u_department = gs.getUser().getDepartmentID();
Example 4: Show/Hide Field for ITIL Users
if(g_user.hasRole('itil'))
{
g_form.setVisible('impact', true);
} else
{
g_form.setVisible('impact', false);
}
5. Best Practices
- Use server-side methods whenever possible (performance is better).
- Use g_user only for quick, client-side checks.
- Avoid heavy GlideRecord queries on the client side.
- Use hasRoleExactly() when you want strict role validation (ignoring admin override).
// Example 5: Check Group Membership (Server-side)
var grpName = 'Service Desk';
var usrID = gs.getUserID(); // Get current user sys_id
var grp = new GlideRecord('sys_user_grmember');
grp.addQuery('group.name', grpName);
grp.addQuery('user', usrID);
grp.query();
if (grp.next()) {
gs.info('User belongs to Service Desk');
} else {
gs.info('User does not belong to Service Desk');
}
-------------- Thanks Everyone For Reading this article ------------------
https://www.servicenow.com/community/itsm-articles/mastering-user-objects-in-servicenow-a-complete-guide-with-real/ta-p/3356552