The CreatorCon Call for Content is officially open! Get started here.

Ravi Gaurav
Giga Sage
Giga Sage

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:

  1. GlideSystem (gs) User Object - Server-side

  2. 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 ------------------

 

Comments
malvikanigam
Giga Explorer

Super Thanks @Ravi Gaurav 

bushlauren1
Tera Explorer

Thanks

Rampriya-S
Tera Guru
Tera Guru

Thanks for consolidating the user objects and methods, Ravi—these are really important to keep handy in a developer’s day-to-day work.

Version history
Last update:
‎08-20-2025 03:03 AM
Updated by:
Contributors