I want to get logged in user information in a client script.

Takumi Togashi
Tera Expert

I would like to get the constant for a logged in user in a client script.

How do I get the email address, user ID, etc.?

3 REPLIES 3

Community Alums
Not applicable

Hi @Takumi Togashi ,

The g_user object can be used only in UI policies and Client scripts. Contrary to its naming, it is not truly a user object. g_user is actually just a handful of cached user properties that are accessible to client-side JavaScript. This eliminates the need for most GlideRecord queries from the client to get user information (which can incur a fairly significant performance hit if not used judiciously).

g_user Property or Method Return value
g_user.userName User name of the current user e.g. employee
g_user.firstName First name of the current user e.g. Joe
g_user.lastName Last name of the current user e.g. Employee
g_user.userID sys_id of the current user e.g. 681ccaf9c0a8016400b98a06818d57c7
g_user.hasRole() True if the current user has the role specified, false otherwise. ALWAYS returns true if the user has the 'admin' role.

Usage: g_user.hasRole('itil')
g_user.hasRoleExactly() True if the current user has the exact role specified, false otherwise, regardless of 'admin' role.

Usage: g_user.hasRoleExactly('itil')
g_user.hasRoles() True if the current user has at least one role specified, false otherwise.

Usage: g_user.hasRoles('itil','admin')

 

 

Also, Refer to the thread for example : https://www.servicenow.com/community/developer-articles/get-user-details-based-on-the-logged-in-user...

 

Jaspal Singh
Mega Patron
Mega Patron

Hi Takumi,

In addition to what is suggested in comments before you can use a combination of Script include (write once & use as many time in as many places) & Client script for the same.

Refer article link for a check.

Amit Gujarathi
Giga Sage
Giga Sage

HI @Takumi Togashi ,
I trust you are doing great.
to retrieve the email address and user ID of a logged-in user in a client script in ServiceNow, you can use the following code:

// Get the current user's email address
var emailAddress = gs.getUser().getEmail();

// Get the current user's sys_id (user ID)
var userID = gs.getUser().getID();

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi