- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
As ServiceNow developers, if we need a handle to a user record and don't want to perform a GlideRecord query, we have used the getUser() method on the GlideSystem class via the global 'gs' variable. This works well, usually, but I want it to work slightly differently.
For reference, here is a link to the wiki article on Getting a User Object - ServiceNow Wiki
If I do a simple getUser() to get the current user's sys_user record, I have access to some of the user's properties.
var currentUser = gs.getUser();
var email = currentUser.email;
gs.print(email);
However, the OOTB getUser() doesn't support custom fields that you have added to the sys_user table. We have added a u_start_date field to sys_user on our instance. If I try to retrieve it with getUser(), this happens:
var currentUser = gs.getUser();
var startDate = currentUser.u_start_date;
gs.print(startDate);
Strangely, if you want to retrieve a user other than the current user you have to first retrieve the current user. Then you can use that current user object to access the getUserByID() method.
Pasting in the code from the wiki article to see how we would get the first name of the current user and a different user.
var ourUser = gs.getUser();
gs. print( ourUser. getFirstName()); //should print out the first name of the user you are currently logged in as
ourUser = ourUser. getUserByID( 'abel.tuter'); //fetched a different user, using the user_name field or sys_id on the target user record.
gs. print( ourUser. getFirstName()); //this should be the first name of the user you fetched above
You can use both of these objects to access some of the user properties
These work:
var ourUser = gs.getUser('JSMITH1');
gs.print(ourUser.fullName);
gs.print(ourUser.lastName);//doesn't match sys_user field name
gs.print(ourUser.name);
gs.print(ourUser.email);
gs.print(ourUser.displayName);
gs.print(ourUser.title);
gs.print(ourUser.companyID);
gs.print(ourUser.departmentID);
gs.print(ourUser.managerName);
gs.print(ourUser.managerID);
These fail:
var ourUser = gs.getUser('JSMITH1');
gs.print(ourUser.last_name);
gs.print(ourUser.isLockedOut);
gs.print(ourUser.isActive);
So the available properties are really sort of a "crap shoot".
You can dependably get user information for a user using these documented methods:
myUserObject.getFirstName() -- returns first name of current user
myUserObject.getLastName() -- returns the last name of the current user
myUserObject.getFullName() -- returns the current user's full name
myUserObject.getDisplayName() -- returns the current user's display name, typically the same as full name
myUserObject.getEmail() -- returns the email address of the current user
myUserObject.getMobileNumber() — returns the mobile number of the current user
myUserObject.getDepartmentID() -- returns the sys_id of the current user's department
myUserObject.getCompanyID() -- returns the sys_id of the current user's company
myUserObject.getCompanyRecord() -- returns the current user's company GlideRecord
myUserObject.getLanguage() -- returns the current user's language
myUserObject.getLocation() -- returns the current user's location
myUserObject.getCountry() -- returns the current user's country
myUserObject.getManagerName() -- returns the full name of the current user's manager
myUserObject.getManagerID() -- returns the sys_id of the current user's manager
myUserObject.getDomainID() -- returns the domain ID of the current user
myUserObject.getDomainDisplayValue() -- returns the domain display value for the current user
myUserObject.getTZ() -- returns the timezone of the current user
myUserObject.getUserRoles() -- returns the roles explicitly granted to the current user
That small list of properties is useful, but use-cases often arise outside of this subset, forcing us to perform a GlideRecord query on the sys_user table to get what we need.
So here is my list of things about getUser() that I feel need improvement:
- It feels awkward to have to get the current user in order to get a different user.
- I really don't like be restricted to using only a small subset of sys_user's properties.
- The getMyGroups() method name seems like a strange choice.
- The fact that I can use .email, but not things like .last_name, is tempting and likely to lead to bugs, as I fail to properly remember which properties are available and which ones aren't.
Now let's not throw the baby out with the bath water. The getUser() method is a great idea. And I absolutely love the isMemberOf() method that it provides. But I want to tweak it slightly.
Here is my suggestion:
GlideSystem.prototype.getUser2 = function (user) {
var usr;
var grUser = new GlideRecord('sys_user');
if (/current/i.test(arguments[0]) || arguments[0] == undefined) {
grUser.addQuery('sys_id', new GlideSystem().getUserID() );
} else {
//select by sys_id, user_name or email. Ensure that email is unique in your instance or remove it.
grUser.addEncodedQuery('sys_id=' + arguments[0] + '^ORuser_name=' + arguments[0] + '^ORemail=' + arguments[0]);
}
grUser.query();
if ( grUser.next() ) {
usr = grUser;
} else {
return null;
}
//Returns comma separated list of group ids
usr.getGroups = function () {
//Use an object key strategy to list group ids and remove duplicates
var grps = [];
var grGrpMember = new GlideRecord('sys_user_grmember');
grGrpMember.addQuery('user', usr.sys_id);
grGrpMember.query();
while (grGrpMember.next()) {
grps.push(grGrpMember.group.sys_id.toString());
}
return grps.toString();
};
//Returns comma separated list of role ids with duplicates removed
usr.getRoles = function () {
//Use an object key strategy to list group ids and remove duplicates
var grps = {};
var grGetRoles = new GlideRecord('sys_user_has_role');
grGetRoles.addQuery('user', usr.sys_id);
grGetRoles.query();
while (grGetRoles.next()) {
grps[grGetRoles.role.sys_id.toString()] = true;
}
var ret = '';
for (var k in grps) {
ret += k + ',';
}
ret = ret.slice(0, -1);//remove last comma
return ret;
};
//Accepts group id or name
usr.isMemberOf = function (group) {
var grIsMember = new GlideRecord('sys_user_grmember');
grIsMember.addQuery('user', usr.sys_id);
var orHandle = grIsMember.addQuery('group.name', group);//passed group name
orHandle.addOrCondition('group',group);//passed sys_id
grIsMember.query();
if (grIsMember.next()) {
return true;
}
return false;
};
//Accepts role id or name
usr.hasRole = function (role) {
var grHasRole = new GlideRecord('sys_user_has_role');
grHasRole.addQuery('user', usr.sys_id);
var orHandle = grHasRole.addQuery('role.name', role);//passed group name
orHandle.addOrCondition('role', role);//passed sys_id
grHasRole.query();
if (grHasRole.next()) {
return true;
}
return false;
};
return usr;
}
The getUser2() method allows us to get the current user like this:
var currentUser = gs.getUser2('current');
OR
var currentUser = gs.getUser2();
But we can also go straight for a different user without first going through the unnecessary step of getting the current user:
var otherUser = gs.getUser2('JSMITH1');
To get a comma separated list of the user's groups:
var usersGroups = otherUser.getGroups(); //This stores a comma separated list of group sys_ids in a string.
This could occasionally be helpful, but we are likely more often going to be checking to see if the user is a member of a particular group.
if(gs.getUser2().isMemberOf('SNOW Dev')){
gs.print('User is member of SN Dev Group');
}
To get a comma separated list of the user's roles (without duplicates):
var usersRoles = gs.getUser2().getRoles(); //This stores a comma separated list of role sys_ids in a string.
To check to see if a retrieved user has a give role:
var hasRole = gs.getUser2('JSMITH1').hasRole('admin'); //returns true if JSMITH1 has the admin role
The advantages of using getUser2():
- You don't have to get the current user before getting the user you really want. (Single step instead of two-step process.)
- You can get to any property on the sys_user table, even custom properties you have added to the dictionary entry for the table.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.