GlideUser (g_user) API – Complete Guide for ServiceNow CAD Exam
Introduction: Why the GlideUser (g_user) API Matters
The GlideUser API, accessed through the g_user object, is one of the most frequently tested topics on the ServiceNow Certified Application Developer (CAD) exam. Understanding this API is critical because it allows developers to retrieve information about the currently logged-in user directly from client-side scripts. Whether you are controlling UI behavior, conditionally showing or hiding fields, or making role-based decisions in Client Scripts or UI Policies, g_user is the go-to object.
What is the GlideUser (g_user) API?
The g_user object is a client-side JavaScript object available in ServiceNow that provides methods and properties to access information about the currently logged-in user. It is automatically available in all client-side scripts, including:
- Client Scripts (onLoad, onChange, onSubmit, onCellEdit)
- UI Policies (scripted conditions and script sections)
- UI Actions (when configured to run on the client side)
It is important to note that g_user is not the same as GlideRecord queries on the sys_user table. The g_user object is a lightweight, cached representation of the current user's session data and does not make real-time queries to the database.
Key Properties of g_user
The following are the most important properties you should memorize for the exam:
g_user.userName – Returns the user name (login ID) of the currently logged-in user. For example: "admin" or "john.doe".
g_user.userID – Returns the sys_id of the currently logged-in user's record in the sys_user table.
g_user.firstName – Returns the first name of the currently logged-in user.
g_user.lastName – Returns the last name of the currently logged-in user.
Key Methods of g_user
These methods are critical for exam success:
g_user.hasRole('role_name') – Returns true if the current user has the specified role, false otherwise.
Example: g_user.hasRole('itil') returns true if the user has the ITIL role.
Important Exam Note: The hasRole() method always returns true for users with the admin role, regardless of whether they actually have the specified role. This is because the admin role inherits all roles. This is a very commonly tested concept.
g_user.hasRoleExactly('role_name') – Returns true only if the user has the exact specified role. Unlike hasRole(), this method does not return true for admin users unless they explicitly have the specified role. This is the method to use when you need to check for a specific role without the admin override behavior.
g_user.hasRoleFromList('role1, role2, role3') – Returns true if the current user has at least one of the roles specified in the comma-separated list. Like hasRole(), this also returns true for admin users.
g_user.getClientData('key') – Retrieves a value that was previously set using g_user.putClientData('key', 'value') from a server-side script (typically a UI Script or Business Rule using putClientData method from the session object). This is used for passing data from the server to the client.
How g_user Works Under the Hood
When a user logs into ServiceNow, the platform caches certain user attributes and role information in the user's session. The g_user object is populated from this cached session data when a form or list loads on the client side. Because the data is cached:
- It is fast to access (no server round-trips required).
- It may not reflect real-time changes to the user record until the session is refreshed or the user logs out and back in.
- It contains a limited set of user attributes (not all fields from sys_user).
If you need access to additional user fields not available via g_user, you would need to use a GlideAjax call to a server-side Script Include, or use a GlideRecord query in a server-side Business Rule.
Common Use Cases
1. Role-Based Field Visibility:
In an onLoad Client Script, you might use g_user.hasRole('itil') to determine whether to make certain fields visible or mandatory only for ITIL users.
2. Restricting Form Submissions:
In an onSubmit Client Script, you can check if the user has a specific role before allowing the form to be submitted.
3. Personalizing the User Experience:
Using g_user.firstName, you could display a personalized welcome message or greeting on a form.
4. Pre-populating Fields:
You can use g_user.userID to set a reference field to the currently logged-in user.
g_user vs. Server-Side GlideSystem (gs) Methods
A common exam trap is confusing client-side g_user with server-side gs (GlideSystem) methods. Here is a comparison:
- g_user.userName (client-side) is equivalent to gs.getUserName() (server-side)
- g_user.userID (client-side) is equivalent to gs.getUserID() (server-side)
- g_user.hasRole('role') (client-side) is equivalent to gs.hasRole('role') (server-side)
Remember: g_user is only available on the client side. You cannot use it in Business Rules, Script Includes, or other server-side scripts. Similarly, gs is only available on the server side.
Exam Tips: Answering Questions on GlideUser (g_user) API
Tip 1: Know the Scope – Client-Side Only
If an exam question asks about retrieving current user information in a Business Rule or Script Include, the answer will never be g_user. Those are server-side contexts where you must use gs (GlideSystem). Conversely, if the question is about a Client Script or UI Policy, g_user is the correct choice.
Tip 2: hasRole() vs. hasRoleExactly()
This is one of the most commonly tested distinctions. Always remember:
- hasRole() returns true for admin users even if they don't explicitly have the role.
- hasRoleExactly() checks for the exact role only, with no admin override.
If an exam question describes a scenario where admin users should NOT be treated as having a specific role, the answer is hasRoleExactly().
Tip 3: Memorize the Properties
Know the four key properties: userName, userID, firstName, lastName. Note they are properties, not methods – they do not use parentheses. For example, it is g_user.userName, NOT g_user.getUserName().
Tip 4: Watch for Trick Questions About Real-Time Data
If a question asks about retrieving a user's department, manager, email, or other fields NOT included in g_user's standard properties, the answer is likely to involve GlideAjax or a server-side approach. The g_user object has limited data.
Tip 5: hasRoleFromList() Takes a Comma-Separated String
Remember that hasRoleFromList() accepts a single string with comma-separated role names, not an array. Example: g_user.hasRoleFromList('itil, admin, catalog_admin').
Tip 6: getClientData() and putClientData()
If an exam question discusses passing data from a server-side script to a client-side script, and mentions g_user, think about the getClientData() method. The server side uses the session object to call putClientData(), and the client side uses g_user.getClientData() to retrieve it.
Tip 7: Understand the Context of the Question
Read each question carefully to identify whether the scenario is client-side or server-side. Key indicators:
- Client-side: Client Script, UI Policy, UI Action (client checkbox checked), g_form, g_user
- Server-side: Business Rule, Script Include, Scheduled Job, gs, current, previous
Tip 8: Practice with Elimination
On the exam, if you see answer choices mixing g_user methods with gs methods in incorrect contexts, eliminate them immediately. For example, g_user.getUserName() in a Client Script is wrong – it should be g_user.userName (a property, not a method). Similarly, gs.hasRole() in a Client Script is wrong because gs is server-side only.
Summary Table for Quick Review
Property/Method — Returns — Notes
g_user.userName — Login name (string) — Property, not a method
g_user.userID — sys_id (string) — Property, not a method
g_user.firstName — First name (string) — Property, not a method
g_user.lastName — Last name (string) — Property, not a method
g_user.hasRole('role') — true/false — Returns true for admin users always
g_user.hasRoleExactly('role') — true/false — Does NOT return true for admin override
g_user.hasRoleFromList('roles') — true/false — Comma-separated string; true if user has at least one
g_user.getClientData('key') — string value — Retrieves data set by server-side putClientData
Final Thought: The GlideUser (g_user) API is a foundational client-side concept in ServiceNow development. Mastering its properties, methods, and the distinction between hasRole() and hasRoleExactly() will help you confidently answer multiple questions on the CAD exam. Always pay attention to the execution context (client vs. server) and remember that g_user provides cached, limited user data — not a full GlideRecord of the sys_user table.