The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Roles Comparison in Table "sys_app_module"

madhusudanshett
Kilo Contributor

Hello SNC

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('application.title', 'CONTAINS', 'Self Service');

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

Untitled.png

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

User+Roles.png

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 objArray2 = [];

  while(gr11.next()) {

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

  }

Please help me to compare the roles field of "sys_app_module" table with the logged in user roles.

Thanks in Advance.

1 ACCEPTED SOLUTION

Hi Madhusudan,



I've set this up with a custom table just like yours and got it to work, with columns:


  • u_name (string)
  • u_active (boolean)
  • u_roles (list)


There was an error in your previous code leading to an invalid query string in your "tab.addQuery(...)" statement, I've made the correction in bold below and added a table for testing the output:



(screenshot)Untitled.png






<g:evaluate var="jvar_roles">


      gs.getUser().getRoles().toString();


</g:evaluate>



Roles<br />


<j:forEach items="${jvar_roles.toString().split(',')}" var="jvar_role">


      ${jvar_role}<br />            


</j:forEach>



<g:evaluate var="jvar_qp" object="true">


      var gr9 = new GlideRecord('sys_user_role');


      gr9.addQuery('nameIN' + gs.getUser().getRoles().toString());


      gr9.query();    


    var qry1 = '';


      var sep = '';


      while(gr9.next()) {


              qry1 += sep + 'u_roleLIKE' + gr9.sys_id;


              sep = '^OR';


      }


      //qry1;


      //gs.getUser().getRoles().toString();


      var tab = new GlideRecord('u_other_application_menu'); <!-- Custom table to store application names-->


      tab.addQuery('u_active=true^' + qry1);   <!-- Check box: SN Application Menu Item -->


      tab.query();


      //tab.getRowCount();


      tab;


</g:evaluate>



<br />Found records:${jvar_qp.getRowCount()}


<br />Application Names


<br />


<table cellspacing="50" cellpadding="20" border="1" class="background_transparent">


      <j:while test="${jvar_qp.next()}">


              <tr>


                      <td>${jvar_qp.getValue('u_name')}</td>


              </tr>


      </j:while>


</table>






Please give this a try, it has tested successfully and should work for you assuming you copy/paste it and then adapt the output variable "jvar_qp" to your needs.




Good luck,


-Brian


View solution in original post

18 REPLIES 18

Brian Dailey1
Kilo Sage

Hi Madhusudan,



Applications and Modules can have roles associated, and when a user is logged-in they only see the applications/modules that they have a matching role for.   So, I'm not too clear on what your goal is here.   Maybe you could just give us details on what the end result is that you wish to accomplish without delving into the code at all.



But just to go along with the code you have presented above, here is a way you can query all the modules to find those that contain a role matching the user's roles:



//Get the user's roles


var userRoles = gs.getUser().getUserRoles();


arrayRoles = userRoles.toString().split(',');


var criteria = 'rolesLIKE' + arrRoles[0];


for(var i=1;i<arrayRoles.length;i++){


        criteria += '^ORrolesLIKE' + arrRoles[i];


}



var gr = new GlideRecord('sys_app_module');


gr.addQuery('active',true);


gr.addQuery('application.titleLIKESelf Service');


gr.addQuery(criteria);


gr.query();



That should match up any module-owned roles with any user-owned roles.




-Brian


Hi Brian



Thank you for the reply.


Yes. Applications and modules will show up on the left hand side in ServiceNow once user logs in.



Let me explain you about my requirement.



Module1.png


For Matthew these many applications will show up when he logs in. I want to display these applications to show as a drop-down under "Other applications" in a Home page.


Module3.png


Business team wants to decide which applications they want to show under Other applications from the available options.


So I have created the table(module:Other application menus) to store the Application names and from there business team can select which application they want to display by making a checkmark on check box(SN Application Menu Item). Application role is given in Role field of the module. Role field of custom table is of type List.


Module2.png


I have written the following code to query the custom table first,to check for the availability of the applications and then to display the modules under that application.



<li class="parent-dropdown">


<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">${gs.getMessage('Other Applications')}</a>



<g:evaluate var="jvar_qp" jelly="true" object="true">


var gr9 = new GlideRecord('sys_user_role');


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


gr9.query();


gr9;



var qry1;


var objArray1 = [];


while(gr9.next()) {


objArray1.push(gr9.sys_id.toString());   <!-- Storing the user role sys_id in an array -->


}



var tab = new GlideRecord('u_other_application_menu'); <! Custom table to store application names-->


tab.addQuery('u_active',true);   <!-- Check box: SN Application Menu Item -->



  for (i = 0; i != objArray1.length; i++){


                  if(!qry1){


      qry1 = 'u_roleLIKE' + objArray1[i];  


                  }else{


                            qry1 += '^ORu_roleLIKE' + objArray1[i];


                  }


        }


gs.log("Query Strg:" + qry1);


tab.addEncodedQuery(qry1);   <1-- Encoded query to compare User Role with Role filed of custom table. -->


tab.query();


tab;


</g:evaluate>


</j:if>



<ul class="dropdown-menu multi-level">


    <j:while test="${tab.next()}">


  <li class="dropdown-submenu">


  <a href="#" class="dropdown-toggle" data-toggle="dropdown">${tab.u_url_name.getDisplayValue()}</a> <!-- Displaying the name of the application from custom table-->



<!-- From here I am using the code given by you now -->


<g:evaluate var="jvar_app3" jelly="true" object="true">


            var userRoles = gs.getUser().getUserRoles();


            arrayRoles = userRoles.toString().split(',');


            var criteria = 'rolesLIKE' + arrRoles[0];


            for(var i=1;i!=arrayRoles.length;i++){


                    criteria += '^ORrolesLIKE' + arrRoles[i];


            }



  var app1=new GlideRecord('sys_app_module');


  app1.addQuery('active',true);


  app1.addQuery('application.title',tab.u_url_name); <!--From previous <g:evaluate> :tab.u_url_name or jelly.jvar_qp.u_url_name-->


app1.addEncodedQuery(criteria);   <!-- Is it addQuery or addEncodedQuery here? -->


  app1.query();


  app1;


  </g:evaluate>



  <ul class="dropdown-menu">


  <j:while test="${app1.next()}">


  <li><a href="">${app1.title}</a></li> <!-- Displaying Module name under Application -->


</j:while></ul></li></ul></li>



Please let me know where I am making mistakes and help me to correct the mistakes.



Thank you very much.


Hi Madhusudan,




1. First of all, using "gs.getUser().getRoles()" will return a comma separated list of roles by name, so this does not work:



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



Instead, try:


      gr9.addQuery('nameIN' + gs.getUser().getRoles().toString());




2. Then after "gr9.query();" you don't need "gr9;" to pass the GlideRecord down the line... I don't think this is what your evaluating jvar_qp to, and it is not the last line in your evaluate block.





Looking at that first section, I would rearrange to this:



<g:evaluate var="jvar_qp" jelly="true" object="true">


var gr9 = new GlideRecord('sys_user_role');


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


gr9.query();



var qry1;


var sep = '';


while(gr9.next()) {


      qry1 = sep + 'u_roleLIKE' + gr9.sys_id;


      sep = '^OR';


}



gs.log("Query Strg:" + qry1);



var tab = new GlideRecord('u_other_application_menu'); <! Custom table to store application names-->


tab.addQuery('u_active=true^' + qry1);   <!-- Check box: SN Application Menu Item -->


tab.query();


tab;



</g:evaluate>



</j:if><!-- WHAT DOES THIS BELONG TO   (there is no beginning j:if tag)-->




You have a lot going on,   I would suggest breaking it down into smaller chunks and making each one works before trying to combine everything into your end product.   The second portion has unclosed tags (<j:while>), etc.



Try getting just one portion (e.g., the first section) of your code working, just output the values as text to the page or something as a test... then move on to the next part.




Thanks,


-Brian


Hi Brian



As per your suggestion I am using the following code:



<g:evaluate var="jvar_qp" jelly="true" object="true">


var gr9 = new GlideRecord('sys_user_role');


gr9.addQuery('nameIN' + gs.getUser().getRoles().toString());


gr9.query();



var qry1;


var sep = '';


while(gr9.next()) {


      qry1 = sep + 'u_roleLIKE' + gr9.sys_id;


      sep = '^OR';


}


gs.log("Query Strg:" + qry1);



var tab = new GlideRecord('u_other_application_menu'); <!-- Custom table to store application names-->


tab.addQuery('u_active=true^' + qry1);   <!-- Check box: SN Application Menu Item -->


tab.query();


tab;


</g:evaluate>



It doesn't display the applications which are selected as true in   "u_other_application_menu" table in drop-downs.



In previous code , it was displaying the drop-downs for applications (not for modules).


<g:evaluate var="jvar_qp" jelly="true" object="true">


var gr9 = new GlideRecord('sys_user_role');


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


gr9.query();



var qry1;


var objArray1 = [];


while(gr9.next()) {


objArray1.push(gr9.sys_id.toString());   <!-- Storing the user role sys_id in an array -->


}



var tab = new GlideRecord('u_other_application_menu'); <! Custom table to store application names-->


tab.addQuery('u_active',true);   <!-- Check box: SN Application Menu Item -->



  for (i = 0; i != objArray1.length; i++){


                  if(!qry1){


      qry1 = 'u_roleLIKE' + objArray1[i];


                  }else{


                            qry1 += '^ORu_roleLIKE' + objArray1[i];


                  }


        }


gs.log("Query Strg:" + qry1);


tab.addEncodedQuery(qry1);   <1-- Encoded query to compare User Role with Role filed of custom table. -->


tab.query();


tab;


</g:evaluate>



Please help me with this.



Thank you very much.