Mastering User Objects in ServiceNow – A Complete Guide with Real Examples

Ravi Gaurav
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')) {...}

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

 

7 REPLIES 7

Dr Atul - LNG
Tera Patron

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?

Hey @Dr Atul - LNG  I choose Article only.. And it creating a question again.. I have highlighted this to admin as well.

Let me put this in Blog instead @Dr Atul - LNG 

Ravi Gaurav
Giga Sage

@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