Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

gs.getUser() accept parameters?

xiaix
Tera Guru

I'd like to pass a sys_user GlideRecord object to "gs.getUser()".  Is that possible?

For example, in a script include:

var u = new GlideRecord('sys_user');
u.get('92ece4dfdb3dd41084cc74b5ae9619d7');
if (u) {
	if (gs.getUser(u).isMember('Service Desk')) {
		/* do something */
	}
}

That possible?

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

No. There are no parameters to gs.getUser().

 

View solution in original post

11 REPLIES 11

Ashley Snyder1
Giga Guru

This is a great bookmark to have: https://www.servicenowguru.com/scripting/user-object-cheat-sheet/

You can use isMemberOf, here's the example from the post:

if(gs.getUser().isMemberOf(current.assignment_group)){
//Do something...
}

var isMember = gs.getUser().isMemberOf('Hardware');

To do this for a user that isn't the currently logged-in user...

var user = 'admin';
var group = "Hardware";
if (gs.getUser().getUserByID(user).isMemberOf(group)){
gs.log( gr.user_name + " is a member of " + group);
}

else{
gs.log( gr.user_name + " is NOT a member of " + group);
}

Yep, I reference that all the time  🙂 

Nothing there that I found answered my question.

Ah, looking at your code, you're not getting the current logged in user, you're trying to GlideRecord for a specific user, in this case your GlideRecord query would use the sys_user_grpmember table instead and plug in the addQueries for both the user and group.

 

var userGr = new GlideRecord("sys_user_grmember");


userGr.addQuery('group.sys_id',grp_sys_id);


userGr.addQuery('user.sys_id',user_sys_id);


userGr.query();


while (userGr.next()) {

//Do something

}

Yeah, that's what I thought.. hehe.   I was trying to 'shortcut' it 😛

I figured though that I'd have to go the long way around.

Thanks!