How to traverse user role from getUserRoles() function?

kai6novice
Kilo Contributor

I am getting userRoles from:

var thisUser = gs.getUser();

var userRoles = thisUser.getUserRoles();

//var userRoles2 = thisUser.getRoles(); //this doesn't really work, because when I turn debug log on, and call gs.print(userRoles2); it only return one role.

gs.print(userRoles); //this return 2 roles.

Now I would like to traverse the role, but no matter what I try, i failed.

//gs.print(userRoles.length); failed, maybe because userRoles is not an array?

//for (role in userRoles){gs.print(role)}; failed, again because userRoles is not an array.

9 REPLIES 9

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Try this:


var thisUser = gs.getUser();


var myArray = thisUser.getUserRoles().toString().split(",");



for (i=0; i<myArray.length;i++) {


gs.log(myArray[i]);


}




One thing that I can't explain is that getRoles() returns more roles than getUserRoles(). No idea why.



Perhaps something you know kalai ?



//Göran


getRoles() - Gives all the roles that the user has.


getUserRoles() - Gives only the roles that were manually assigned to the user (not inherited from any roles or groups)


Hi Kalaiarasan,



      Where did you find the API? I couldn't find any documentation explaining what type of object it's returning.


      And for some reason, I get more role from getUserRoles() than getRoles().



Thanks,


Kai