Determining if a user has no roles

James Bengel
Giga Expert

I'm relatively new at SN, and working on User Criteria for Knowledge. The requirement is for users with a specified company that have no roles (explicit or inherited). The company I can pick using the selector, but the absence of roles looks like it would require a bit of a script.  This seems like it should work:

answer = !(gs.getUser().getRoles().toString().length); // return true if no roles are present

but if anyone has a better idea (ideally without a script at all) I'd love to hear it.

I'm skeptical about using .length without explicitly converting to String first (I've been burned by such before) but the User Roles type is described as "extends String", so it's neither a string nor an array.  Since I only care that there's either something or nothing in the list, I don't need it to be an array, but I do need to be able to rely on the .length property working correctly.

1 ACCEPTED SOLUTION

Pretty sure that would work, but after working around Glide for a few months now, I think what might be cleaner is something like:

var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', gs.getUserID());
gr.query();
answer = gr.hasNext()



View solution in original post

3 REPLIES 3

ggg
Giga Guru

this does the work:

var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', 'bfb9aa5bdbf323003660a0f2ca961966');
gr.query();
if (!gr.next()){
    gs.log('user has no roles');
}

Pretty sure that would work, but after working around Glide for a few months now, I think what might be cleaner is something like:

var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', gs.getUserID());
gr.query();
answer = gr.hasNext()



your script is better and correct. thanks!