User Object Cheat Sheet25 February 2010 N Mark Stanger 24 Comments o matter what system you’re working in, it is always critical to be able to identify information about the user who is accessing that system. Being able to identify who the user is, what their groups and/or roles are, and what other attributes their user record has are all important pieces of information that allow you to provide that user with a good experience (without giving them information they don’t need to have or shouldn’t have). ServiceNow gives administrators some pretty simple ways to identify this information in the form of a couple of user objects and corresponding methods. This article describes the functions and methods you can use to get information about the users accessing your system. GlideSystem User Object The GlideSystem (gs) user object is designed to be used in any server-side JavaScript (Business rules, UI Actions, System security, etc.). The following table shows how to use this object and its corresponding functions and methods. Function/Method Return Value Usage gs.getUser() Returns a reference to the user object for the currently logged-in user. var userObject = gs.getUser(); gs.getUserByID() Returns a reference to the user object for the user ID (or sys_id) provided. var userObject = gs.getUser().getUserByID('employee'); gs.getUserName() Returns the User ID (user_name) for the currently logged-in user. var user_name = gs.getUserName(); e.g. 'employee' gs.getUserDisplayName() Returns the display value for the currently logged-in user. var userDisplay = gs.getUserDisplayName(); e.g. 'Joe Employee' gs.getUserID() Returns the sys_id string value for the currently logged-in user. var userID = gs.getUserID(); isMemberOf() Returns true if the user is a member of the given group. var location = gs. getManagerID() Returns the manager sys_id of the currently logged-in user. var company = gs. getDomainID() Returns the domain sys_id of the currently logged-in user (only used for instances using domain var domainID = gs.getManagerID().getEmail().getUser(). gs.getUser().getDomainDisplayValue().getCompanyID(). getEmail() Returns the email address of the currently logged-in user.getFirstName().getDepartmentID(). getLocation() Returns the location of the currently logged-in user. getMyGroups() Returns a list of all groups that the currently logged-in user is a member of. To do this for a user that isn't the currently logged-in . getDepartmentID() Returns the department sys_id of the currently logged-in user.. var companyID = gs.getUser().getLastName().getCompanyRecord(). Takes either a group sys_id or a group name as an argument.getLanguage(). } var isMember = gs.getUser(). var email = gs.getUser().getDomainID(). false otherwise.. if(gs.getUser(). getDomainDisplayValue() Returns the domain display value of the currently logged-in user (only used for instances using var domainName = domain separation).isMemberOf(current.getUser().getUser().isMemberOf('Hardware').assignment_group)) { //Do something.getLocation(). getLanguage() Returns the language of the currently logged-in user.getUser(). getLastName() Returns the last name of the currently logged-in user. var lastName = gs. getCompanyRecord() Returns the company GlideRecord of the currently logged-in user.getUser().getUser().getUser(). getCompanyID() Returns the company sys_id of the currently logged-in user. separation).getUser(). var managerID = gs. var groups = gs. var language = gs.getFirstName() Returns the first name of the currently logged-in user. var firstName = gs.getUser(). var deptID = gs.getMyGroups(). } gs.hasRoles()){ //User is an ess user.next()..hasRole('itil')){ //Do something.getValue('title'). var group = "Hardware".log( gr.getUser(). gr.. ..query().addQuery("user_name". } hasRoles() Returns true if the user has any roles at all.. } else{ gs.hasRole() Returns true if the user has the given role.getUser().user_name + " is a member of " + group). //Query for the user record var gr = new GlideRecord("sys_user"). false otherwise.hasRole('itil.admin')){ //If user has 'itil' OR 'admin' role then Do something.log( gr. } gs. false if the user has no role (i.e. "admin").getUser().getRecord()’ call as shown here… //This script gets the user's title gs. an ess user). if(gs.. if(gs. gr.isMemberOf(group) ){ gs.hasRole() Returns true if the user has one of the given roles. } It is also very simple to get user information even if the attribute you want to retrieve is not listed above by using a ‘gs.user. if (gs.getUser().getRecord()..getUserByID('admin')..user_name + " is NOT a member of " + group). gr. false otherwise. if(!gs.. g_user Property or Method Return value g_user. Usage: g_user.hasRoleExactly('itil') g_user.g.'admin') It is often necessary to determine if a user is a member of a given group from the client as well. Usage: g_user.g.hasRole() True if the current user has the role specified.hasRoleExactly() True if the current user has the exact role specified.hasRoles('itil'. it is not truly a user object.g. employee g_user. ALWAYS returns true if the user has the 'admin' role. regardless of 'admin' role. Joe g_user.userID sys_id of the current user e.g_user User Object The g_user object can be used only in UI policies and Client scripts. false otherwise. you can get the information by performing a GlideRecord query.hasRoles() True if the current user has at least one role specified. 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).lastName Last name of the current user e. false otherwise.g.userName User name of the current user e. 681ccaf9c0a8016400b98a06818d57c7 g_user. g_user is actually just a handful of cached user properties that are accessible to client-side JavaScript.hasRole('itil') g_user. Although there is no convenience method for determining this from the client. Here’s an example… //Check to see if assigned to is a member of selected group . Contrary to its naming. Usage: g_user. false otherwise. Employee g_user.firstName First name of the current user e. you need to use a GlideRecord query.next()){ //Do something alert('Is a member').name'.query(groupMemberCallback). usrID). alert(title). For the situations where there’s no way around it. you should use a server-side technique described above since GlideRecord queries can have performance implications when initiated from a client script.userID.title. var title = gr.var grpName = 'YOURGROUPNAMEHERE'. } else else{ alert('Is not a member'). gr.addQuery('user'. grp.addQuery('group. grp. grpName). //This script gets the user's title var gr = new GlideRecord('sys_user'). If at all possible. These scripts can be found on the ServiceNow wiki. grp. 24 Comments . //Get current user ID var grp = new GlideRecord('sys_user_grmember').userID).get(g_user. Useful Scripts There are quite a few documented examples of some common uses of these script methods. you could use a script similar to the one shown below to query from the client for more information about the currently logged in user. } } To get any additional information about the currently logged-in user from a client-script or UI policy. function groupMemberCallback(grp){ //If user is a member of selected group if if(grp. var usrID = g_form. you may run into issues.ZoneInfo java object.getSession(). // sun. use the following: var tzStr = gs. Thanks! Reply valor 10-01-2013.getValue(‘time_zone’). 04:55 Thanks for the great tips… My personal favorite: gs. but not their default.getUser(). The time zone selector sets the User’s Session TZ. // “US/Pacific” var jTZ = gs. For more consistent results.docjar.util. 07:20 Thank you for the great work… finally one place to get the information about the UserObject stuff… . never knew you could do that. 12:00 Note that if you use this method.html Reply Wolfgang 25-10-2011..getUser(). APIDoc linked below Java doc ZoneInfo api: http://www.getRecord().getTimeZone().getSession().calendar.! javascript:gs. Looked everywhere and it was on my favorite site.com/docs/api/sun/util/calendar/ZoneInfo. and you have the time zone selector turned on.getRecord().Joe 26-02-2010.getTimeZoneName().getValue(‘title’). awesome! Reply Alex 23-03-2011. 00:44 I was looking for a way to get the default set to the User Time Zone. servicenow. Please .Just one note: – the hasRole() returns true.g. Result is "undefined" var usr2 = gs. Result is "true" Reply Mark Stanger 07-02-2013. I just added it to the article above.print(usr.php?title=Getting_a_User_Object&redirect=no#Method_Detail) but I cannot get it to work.getUser(). gs. Reply Mark Stanger 25-10-2011. I haven’t heard of it being retired. which will only return true. 14:20 I looked at the Usage example for “isMemberOf()” and also checked the wiki article for the function “getUserByID” (http://wiki. 08:04 Thanks for the feedback. if the user has the role specified (e.g. //e. Reply Andreas 07-02-2013. //Result: "object" gs.com/index.hasRoles()). this GlideSystem function for the user object is not available. //Result: "null" gs.type_of(usr)).hasRoles()).print(JSUtil. Is this function maybe retired? Some code I played with is: var usr = gs. – to get a true/false for a specific role. hasRole(role)) OR the admin role. This method had some issues in the past (which is why I didn’t include it). 17:23 I’m not having any luck with it either. but you’ll probably need to contact ServiceNow support to see.type_of(usr2)). if the user has the itil-role.print(usr2.print(JSUtil. gs. //here the GlideSystem function for the user object is available. It looks like it’s in the official ServiceNow doc now though so hopefully I’m safe in including it here :).getUserByID('guest'). use hasRoleExactly(role). anglin'). 08:16 . 07:21 is it possible to check whether he/she is one of the member of Assignment group? Please guide me friends. Reply Mark Stanger 12-04-2013. 08:12 Thanks Andreas! I’ve updated the table above with this solution.isMemberOf(group)) { gs. 07:45 Here we go: first we have to initialize a variable with “getUser()”. } Reply Mark Stanger 08-02-2013.post back here with your findings.log(usr.getUser(). Reply Abdul Fathah 12-04-2013. Below the updated code to make the example work: //get object for specific user var usr = gs.getUserByID('beth. //instead of UserID it is possible to use the sys_id var group = 'Service Desk'. } else { gs.getName() + " is NOT a member of " + group).log(usr.getName() + " is a member of " + group). After that we can get any user object using it’s sys_id or UserID. if (usr. usr = usr. Reply Andreas 08-02-2013. com/scripting/client-scripts-scripting/gform-getreference-callback/ Reply Abdul Fathah 12-04-2013. Thanks for all the info. Thanks Martin Reply . 09:01 Thanks Mark. Martin Robinson 08-10-2013. 06:49 Hi Mark. One question though. Check out ‘isMemberOf’ above. You could easily do a client-side GlideRecord query against the ‘sys_user_grmember’ table to find that though.servicenowguru. 08:53 Not directly. is it possible to do this in client script? Reply Mark Stanger 12-04-2013. You can find an example of this type of script here… http://www.Yes. i forget to mention. Reply Abdul Fathah 12-04-2013. Just make sure to do an asynchronous gliderecord query with a callback. 08:50 sorry Mark. very handy and interesting indeed. is there a method like the var hasRoleExactly that will work server-side? I can get it to work okay client-side but that’s it. 12:28 Up above I explain for both back-end and client-side code how you can get any attribute even if there’s not a specific function defined. 11:28 How does one get the client’s business number? The wiki shows how to get the mobile number. with the following script? current.servicenow. 12:32 If I understand that correctly.getUser().Mark Stanger 08-10-2013. 08:25 Thanks Mark. I thought that might be the alternative ! Cheers Reply Shane C 29-01-2014.getUser().com/index. but not the business number. http://wiki. to set the value of phone for the incident.getRecord().php?title=Getting_a_User_Object Reply Mark Stanger 29-01-2014.getValue(‘phone’). but you could always query the ‘sys_user_has_role’ table directly or create an on-demand script include that would allow you to call it in a shorthand way.getRecord(). . You can get any attribute from the back-end like this. Reply Shane C 29-01-2014.u_cos_contact_no = gs. Reply Martin 08-10-2013. 08:22 I don’t know of any built-in way of doing this.getValue(‘title’). gs. to the user’s business phone. then I could use this in a script of a workflow that works on a condition of an incident being submitted. but I can’t figure out how to edit my previous post. 12:38 On my incident form. would I want to do a custom script.getUser().getRecord().getRecord().getValue(‘phone’). Not sure if it will work. current. I made a script step in the workflow. Any way to modify this? . and the following works in my system: var businessphone = gs.u_cos_contact_no = businessphone. 12:34 Correct. javascript:gs. and then use javascript:gs.getValue(‘phone’) ? Shane C 29-01-2014.getRecord(). Now to try it as a Set Value step in the workflow. Reply Shane C 29-01-2014. As long as ‘phone’ is the name of the field you want to pull the value from. For my workflow. with javascript:gs.getUser().getUser().Reply Mark Stanger 29-01-2014. 12:44 An update. but it’s work a shot to eliminate extra steps in the workflow. not the phone number of who the “client” has been set to for the incident. however it pulls the phone number of the user logging in the incident. In my client’s user form. Shane C 29-01-2014. “Business Phone” variable is called phone. On further testing.getUser().getValue(‘phone’).getRecord().getValue(‘phone’) does work in a Set Value step of the workflow. or simply put do a Set Values. “Contact Phone” variable is called u_cos_contact_no. 12:48 Sorry to keep filling this up. Much thanks Mark .