Comparison of Roles

madhusudanshett
Kilo Contributor

Hi Everyone

Using the following code I am capturing the sys_id of the logged in user in an array.

var gr11 = new GlideRecord('sys_user_role');

  gr11.addQuery('name', gs.getUser().getRoles());

  gr11.query();

  gr11;

  var qry2;

  var objArray2 = [];

  while(gr11.next()) {

  objArray2.push(gr11.sys_id.toString());

  }

We have a role field in "sys_app_module" table of type "User Roles".

User Roles.png

How can I compare the roles field of sys_app_module table with the values(sys_ids) in an array?

ChrisB

Please assist me on this.

Thank you

5 REPLIES 5

Madhu80
ServiceNow Employee
ServiceNow Employee

Madhusudan, are you trying to check if the current user has at least one of the roles specified for a particular sys_app_module entry?



If so you could use, something like



var appId = ''; // Sys id of app


var gr = new GlideRecord('sys_app_module');


gr.get(appId);


var roles = gr.getValue('roles'); // this would return something like 'itil,project_user'


var hasAccess = gs.getUser().hasRole(roles);


Hi



Thanks for the reply.


I don't want to hardcode with sys_id because application changes here.



Thank you


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hey Madhu S,



Can you please tell us the exact requirement you are trying to achieve so that we can assist you further.


Hi Pradeep



I want to display the modules on the page based on the logged in user role.


So I want to compare logged in user role with roles field of module.



var gr = new GlideRecord('sys_app_module');


gr.addQuery('active',true);


gr.addQuery('roles',???)   //comparing logged in user role with roles field of module here



Untitled.png



Please assist me on this.



Thanks in advance.