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

Hi Brian



I have removed ^ from tab.addQuery('u_active=true^' + qry1);. It displays application name from "u_other_application_menu" table but irrespective of the roles assigned to application in "u_other_application_menu" table.



Please help me.


Thank you


Hi Madhusudan,



  1. An invalid query string will return all records (basically, it ignores the criteria), so this (displaying all regardless of roles) points to your query string being incorrect somehow.   Try setting a variable equal to the query string first and just express it as text on the page to verify.

          e.g.,           var criteria = 'u_active=true^' + qry1;
  2. Go to your custom table [u_other_application_menu] and use the Filter to build up your query manually.   Once you run the filter, right-click on the last term in the filter bread-crumbs and choose "Copy query" to copy the query string as text.   Compare the structure of this query string to the one you are composing in the code to see if it is setup correctly.
    (See ref here:   Encoded Query Strings - ServiceNow Wiki )




-Brian        


Hi Brian



I using tab.addQuery('u_active=true^' + qry1); , Drop-down shows empty.



Manual query looks like this : u_active=true^u_roleLIKE6a3e05d8092a594006794ce6479a9875



What changes I need to make?



How to make modules to be displayed ?



Please help me .



Thank you


Hi Brian



Could you please help me to display the modules based on the roles of the logged in user?



Any help is really appreciated.


Thank you


Hi Madhusudan,



I haven't forgotten about your issue... but I can't think of the reason why it's not working for you.   I'm just waiting till I have the time to setup the same scenario you are trying on an instance to test.




Thanks,


-Brian