
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2015 02:31 AM
I have a template system in place which checks the current users groups and only shows them templates that are assigned to those groups.
My issue is if a user is in 2 or more groups that have access to these templates then the template is shown multiple times in their list.
This is the business rule I use to set the filter for these templates:
roTemplates();
function roTemplates(){
//Enforce row-level read permissions for system templates
if(gs.getSession().isInteractive()){
var answer = '';
//Check to see if the user can read all templates
if(!gs.hasRole('template_editor_global')){
//Check to see if the user can read some templates
if(gs.hasRole('template_editor_group') || gs.hasRole('template_editor')){
//User can read templates for themselves, their groups, or global
answer = 'active=true^user=javascript:gs.getUserID()^ORu_groupsLIKEjavascript:getMyGroups()';
current.addEncodedQuery(answer);
}
//Else user cannot read any templates
else{
current.addEncodedQuery('active=true^active=false');
}
}
}
}
Line 11 sets the filter.
Can anyone see a way to remove duplicates in the returned records?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2015 06:10 AM
If any one is interested this is my solution...
roTemplates();
function roTemplates(){
//Enforce row-level read permissions for system templates
if(gs.getSession().isInteractive()){
var answer = '';
//Check to see if the user can read all templates
if(!gs.hasRole('template_editor_global')){
//Check to see if the user can read some templates
if(gs.hasRole('template_editor_group') || gs.hasRole('template_editor')){
//User can read templates for themselves, their groups, or global
var myUserObject = gs.getUser();
var myUserGroups = myUserObject.getMyGroups();
var arrUtil = new ArrayUtil();
var tempArray = [];
var gr = new GlideRecord('sys_template');
gr.addQuery('u_groups','IN', myUserGroups);
gr.query();
while (gr.next()){
tempArray.push(gr.name);
}
tempArray = arrUtil.unique(tempArray);
answer = 'active=true^user=javascript:gs.getUserID()^ORnameIN'+tempArray;
current.addEncodedQuery(answer);
}
//Else user cannot read any templates
else{
current.addEncodedQuery('active=true^active=false');
}
}
}
}
Thanks for the help guys.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2015 02:44 AM
Hi Daryll,
First get the the all users from groups and make it unique means eliminate duplicate user using this below method.
var arrayUtil = new ArrayUtil();
var a1 = new Array("a", "b", "c", "c", "b"); // Suppose if you get this duplicate users
gs.print(arrayUtil.unique(a1));
Output : a,c,b // Here its eliminated duplicate users
For more information click this below link
http://wiki.servicenow.com/index.php?title=ArrayUtil
Regards,
Harish.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2015 04:32 AM
pass the return value of the getMyGroups function to ArrayUtil as suggested by Harish ...

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2015 06:10 AM
If any one is interested this is my solution...
roTemplates();
function roTemplates(){
//Enforce row-level read permissions for system templates
if(gs.getSession().isInteractive()){
var answer = '';
//Check to see if the user can read all templates
if(!gs.hasRole('template_editor_global')){
//Check to see if the user can read some templates
if(gs.hasRole('template_editor_group') || gs.hasRole('template_editor')){
//User can read templates for themselves, their groups, or global
var myUserObject = gs.getUser();
var myUserGroups = myUserObject.getMyGroups();
var arrUtil = new ArrayUtil();
var tempArray = [];
var gr = new GlideRecord('sys_template');
gr.addQuery('u_groups','IN', myUserGroups);
gr.query();
while (gr.next()){
tempArray.push(gr.name);
}
tempArray = arrUtil.unique(tempArray);
answer = 'active=true^user=javascript:gs.getUserID()^ORnameIN'+tempArray;
current.addEncodedQuery(answer);
}
//Else user cannot read any templates
else{
current.addEncodedQuery('active=true^active=false');
}
}
}
}
Thanks for the help guys.