ServiceNow g_user Object

The g_user object is a client-side ServiceNow API that provides selected information about the currently logged-in user. It is useful when a form needs to respond to the user’s identity or roles without making a separate server request for information that is already available in the browser.

1. What is g_user?

In ServiceNow, g_user is a browser-side object that exposes a limited set of cached user properties and role-checking methods. It is commonly used in Client Scripts and client-side UI Actions. Availability can depend on the interface and script context, so test examples in the UI where you intend to use them.

Common uses include:

  • Reading the logged-in user’s username, first name, last name, or sys_id.
  • Checking whether the current user has a role.
  • Adjusting a form experience, such as displaying a message or changing a field value.
  • Avoiding an unnecessary server call when the required user detail is already available client-side.

2. Important limitations

  • Client-side only: do not use g_user in server-side scripts such as Business Rules, Script Includes running on the server, or scheduled jobs.
  • Not a security boundary: hiding a button or field in the browser does not prevent a user from accessing data or invoking server functionality by other means.
  • Use server-side authorization for sensitive operations. Enforce access with ACLs and validate permissions again in server-side logic.
  • Do not assume every property or method is available in every ServiceNow interface. Verify API availability for your release and UI context.
  • Use the exact role name configured in your instance when checking roles.

3. Common g_user properties and methods

Property / methodWhat it doesExample
g_user.userNameReturns the current user’s login username.g_user.userName
g_user.firstNameReturns the current user’s first name.g_user.firstName
g_user.lastNameReturns the current user’s last name.g_user.lastName
g_user.userIDReturns the sys_id of the logged-in user.g_user.userID
g_user.hasRole(‘role_name’)Checks for a role; administrators may return true for role checks.g_user.hasRole(‘itil’)
g_user.hasRoleExactly(‘role_name’)Checks whether the user has that exact role, without the admin shortcut.g_user.hasRoleExactly(‘itil’)
g_user.hasRoles()Returns true when the user has at least one role; otherwise false.g_user.hasRoles()

4. Examples with explanations

Example 1: Read the logged-in user’s username

Use this when you need the user’s login name for a client-side message or a simple form interaction.

var username = g_user.userName;
g_form.addInfoMessage(‘You are signed in as: ‘ + username);

Expected result: the form displays an informational message containing the current user’s username.

Example 2: Display the user’s first and last name

var firstName = g_user.firstName;
var lastName = g_user.lastName;

g_form.addInfoMessage(‘Welcome, ‘ + firstName + ‘ ‘ + lastName + ‘!’);

Expected result: a greeting appears on the form. If a name value is blank in your instance, the message may contain a blank section.

Example 3: Get the logged-in user’s sys_id

The sys_id is the unique record identifier for the user in the User [sys_user] table.

var currentUserId = g_user.userID;
g_form.addInfoMessage(‘Current user sys_id: ‘ + currentUserId);

Use the sys_id when a client-side workflow needs to compare the current user with a reference field. Do not treat a client-provided sys_id as proof of authorization.

Example 4: Check whether the user has a role

if (g_user.hasRole(‘itil’)) {
    g_form.addInfoMessage(‘You have the itil role.’);
} else {
    g_form.addInfoMessage(‘The itil role was not detected.’);
}

Important: hasRole() can return true for an administrator even if the administrator does not explicitly hold the named role. This makes it convenient for general role-aware UI behavior, but not for strict role auditing.

Example 5: Check for an exact role

if (g_user.hasRoleExactly(‘itil’)) {
    g_form.addInfoMessage(‘You have the itil role specifically.’);
} else {
    g_form.addInfoMessage(‘You do not have the itil role specifically.’);
}

Use hasRoleExactly() when you need to distinguish an explicitly assigned role from the administrator role shortcut. Still, do not use this client-side check as the only control for a sensitive action.

Example 6: Check whether the user has any roles

if (g_user.hasRoles()) {
    g_form.addInfoMessage(‘This account has one or more roles.’);
} else {
    g_form.addInfoMessage(‘No roles were detected for this account.’);
}

This method checks whether the current user has at least one role; it does not identify which role is present.

5. Practical lab: personalize a form

Goal: show a welcome message when a user opens a form.

  1. Open ServiceNow and navigate to a table form you can safely use for testing, such as an Incident form in a development or personal developer instance.
  2. Open the form’s context menu and select Configure > Client Scripts, or navigate to System Definition > Client Scripts and create a new Client Script for the table.
  3. Set Type to onLoad. Keep the script limited to a harmless message for this exercise.
  4. Paste the following script, save it, and reload the form.
function onLoad() {
    g_form.addInfoMessage(
        ‘Welcome, ‘ + g_user.firstName + ‘ ‘ + g_user.lastName + ‘!’
    );
}
  • Confirm that the message appears. If it does not, verify that the Client Script is active, applies to the correct table, and is supported in the interface you are testing.

Cleanup: disable or remove the test Client Script after the exercise if you do not want the message on every form load.

6. Practical lab: role-aware form message

Goal: show a different informational message depending on whether the current user has the itil role.

function onLoad() {
    if (g_user.hasRole(‘itil’)) {
        g_form.addInfoMessage(‘ITIL role detected.’);
    } else {
        g_form.addInfoMessage(‘ITIL role not detected.’);
    }
}

Test with accounts that have different roles. Remember that administrators can pass hasRole() checks because of the administrator role behavior. The message is for user experience only, not for restricting access.

7. Common mistakes and troubleshooting

  • Using g_user in a server-side script. It is intended for client-side contexts.
  • Using g_user to secure a record or action. Use ACLs and server-side permission checks instead.
  • Checking the wrong role name. Verify the role’s technical name in your instance.
  • Assuming hasRole() and hasRoleExactly() behave identically for administrators. They do not.
  • Testing only as an admin. Test with a non-admin account to understand actual role behavior.
  • Expecting a client-side API to work in every interface. Test in the target interface and consult documentation for your ServiceNow release.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top