The CreatorCon Call for Content is officially open! Get started here.

How to get a User's roles with a script

Fuse Henry
Kilo Guru

I've been trying unsuccessfully to write a script that give's me a user's roles.  Here's the scenario:


I'm going to the Background - Scripts application in Service Now. Then I run this script:

 

var rolesGR = new GlideRecord("sys_user_has_role");
rolesGR.addquery("sys_user", "Lastname, Firstname"); //I'm putting in the user's actual lastname, firstname
rolesGR.query();
while (rolesGR.next()) {
gs.print(rolesGR.user);
}

I think I'm close. What I need in my query results are the names of the actual ROLES. What I'm getting is a list of system IDs. How to I modify my script to get the actual Role Names for the specific user?

 

If you answer this question: I would greatly appreciate it if you could make sure your answer is complete and contains context. I have been a Service Now developer/admin for only about 4 months, and I'm not likely to understand answers with no context. I would especially appreciate a tweak to my own script (provided above) that renders the desired result. What I really need is the exact script to get a list of a user's roles. Nothing fancy. No extra fluff.
Thank you!

1 REPLY 1

Subhankar Pram1
Tera Contributor

var rolesGR = new GlideRecord("sys_user_has_role");
rolesGR.addquery("sys_user", "Lastname, Firstname"); //I'm putting in the user's actual lastname, firstname
rolesGR.query();
while (rolesGR.next()) {
gs.print(rolesGR.role.name);
}