- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-16-2016 08:19 AM
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.
We have a roles field in "sys_app_module" table of type "User Roles".
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.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-26-2016 07:27 PM
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)
<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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2016 11:32 PM
Hi Madhusudan,
Verified, you can use a query string as follows to include those matching roles and those with no roles:
gr.addQuery('roles=^ORrolesIN' + gs.getUser().getRoles().toString());
Thanks,
-Brian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2016 02:14 AM
Hi Brian
Thank you for the help.
gr.addQuery('roles=^ORrolesIN' + gs.getUser().getRoles().toString()); This query didn't work.
I want to add the following queries in a OR Condition.
gr.addQuery('roles', 'IN', gs.getUser().getRoles().toString()); OR
gr.addNullQuery('roles');
Please help me on this issue.
Thank you

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-29-2016 10:35 AM
Hi Madhusudan,
That is what my last query string does, it adds a null query ("roles=") with your original query as an OR condition. You could switch the order around if you like:
gr.addQuery('rolesIN' + gs.getUser().getUserRoles().toString() + '^ORroles=');
Either way, it is doing the same thing. And I've tested this query string successfully on [sys_app_module], it works as expected. I think what you need to do is put in some debugging code to test your values and make sure you are getting what you expect (e.g., for the query strings, etc.). That way you can check for errors that may be preventing you from getting to your end goal.
In your case above, the method you are calling is not valid... earlier, I gave you this statement to grab a user's roles:
gs.getUser().getUserRoles()
Later in your reply/code, you changed it to:
gs.getUser().getRoles()
This is not a valid method. Unfortunately, in my following replies to you I copied it from your code when making other corrections and didn't catch it either. First, you should check in your Plugins module and look for one called "Contextual Security"...
- If it is listed and active, you should use this statement to get a user's roles: gs.getSession().getRoles();
- If it is not listed (or is not active), you should use this statement to get a user's roles: gs.getUser().getUserRoles();
Next. slow down and make sure the foundation of what you are trying to do is setup properly first, before making the jump to building your structure. Start from zero and build each little piece first to make sure it works (i.e., get your queries working, check the strings, check the results, test them soundly). THEN start piecing them together like Legos (tm) into the final result you are trying to build.
Good luck,
-Brian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-29-2016 10:29 PM
Thank you for the help.