How to query a certain user's role

humblecommitted
Kilo Guru

Hello Community,

I have a question.   I am trying to query a certain role and return a true statement when a user has that certain role.   What am I doing wrong?

Here is the script I am trying to work with

function userHasRole(UserID, role){

var roles = new GlideRecord('sys_user_has_role');

   

roles.addQuery('user', userID);    

roles.addQuery('role.name', change_manager);

roles.query();

   

if (roles == change_manager){

        return 'true';

        }

return 'false';

}

Also you can have a function within a function right?   Here is the whole code I am working on:

answer = ifScript();

          function ifScript() {

                //answer = 'false';

  var userID = gs.getUserID();

   

  gs.log("work start check for " + userID + " assigned_to " + current.assigned_to);

    if (userID == current.assigned_to) {

    return 'true'; }

   

    // query to see if userID has role change management, if so return true

    function userHasRole(UserID, role){

    var roles = new GlideRecord('sys_user_has_role');

   

    roles.addQuery('user', userID);

    roles.addQuery('role.name', change_manager);

    roles.query();

   

    if (roles == change_manager){

    return 'true';

  }

    }

}

  return 'false';

Thanks in advanced.

1 ACCEPTED SOLUTION

humblecommitted
Kilo Guru

Hello Everyone,



So I managed to hash it out.   Here is the code:


answer = ifScript();  


          function ifScript() {  


                //answer = 'false';  


  var userID = gs.getUserID();  


       


  gs.log("work start check for " + userID + " assigned_to " + current.assigned_to);  


    if (userID == current.assigned_to) {  


    return 'true'; }  


       


    // query to see if userID has role change management, if so return true  


 


  var cm_role = new GlideRecord('sys_user_role');


  cm_role.addQuery('name', "change_management");


  cm_role.query();


  while (cm_role.next()) {




  var roles = new GlideRecord('sys_user_has_role');        


  roles.addQuery('user', userID);            


  roles.addQuery('role', cm_role.sys_id);        


  roles.query();        


  while (roles.next()){


  gs.log("work start role check for " + userID + " has role " + cm_role.name);


  return true;


  }


  }


  gs.log("end of work start role check for " + userID + ", no writing allowed" );


      return false;      


}





The errors were as follows:


  1. I had to only use one "answer".   In one variation of the code I had two answers, I could piggy back of the first answer argument.
  2. turns out that sys_user_has_role doesnt have a table named "name".   This is why I had to query the name from the sys_user_role table while grabbing the sys_id of the name of the sys_user_role and populate that in line 20 of the code.

View solution in original post

21 REPLIES 21

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

I'm just curious, why not use the hasRole method?



//Göran


Hello Goran,



Do you have an example code of how this would work?


Sure,



You can for example read about it here: ServiceNow Developers


short version:



g_user.hasRole('change_manager');


or gs.hasRole('change_manager');


Depending if you are using a client script(g_user) or server(gs)


it returns true if the user has the role... But beware, it can fool you when you are admin, it always return true if you are a admin, even if you don't have the role.. No idea the reason behind that thou.



You can also find many functions already using it. For example the OOB ui action "Resolve incident".


Look at it and you will se it be used in the conditions... like this: (current.incident_state != 7 && current.incident_state != 6) && (gs.hasRole("itil") || gs.hasRole("itil_admin"))



//Göran


Bravo Goran,



Wish you hoped on earlier.   BTW the link you provided is dead on my end, may you kindly re-post the link for my review?



Thank you!


Sorry,



Didn't see this post until now



Anyway.. That link was for the developer site and it's docs. try this: https://developer.servicenow.com/app.do and then search for hasRole.



Here you also got a menu to the left where you for example can choose g_user or glidesystem(gs) and then see all the methods for them and most of them also got examples how to use it. I really love that site.



Let me know if there is anything else.



//Göran