Not to Show the roles in list collector that are already present in selected group

Preethi26
Tera Contributor

Hi Team,

I have created catalog request to add roles to groups.

Now, i want to not show the roles that are already in selected group.

Example if group Next gen already has itil role, so i should restrict it in list collector from user selection to avaoid redundant addition.

Kindly help

Preethi26_0-1701412574694.png

 

2 ACCEPTED SOLUTIONS

@Preethi26

then use this in advanced ref qualifier and add this in variable attributes

ref_qual_elements=group

I assume the roles variable refer to sys_user_role table

javascript: var query;
var arr = [];
var option = current.variables.action; // give correct action variable name here
var gr = new GlideRecord("sys_group_has_role");
gr.addQuery("group", current.variables.group); // give correct group variable name here
gr.query();
while (gr.next()) {
	arr.push(gr.getValue('role'));
}

if(option == 'add_role') // give correct choice value to compare
	query = 'sys_idNOT IN' + arr.toString();
else
	query = 'sys_idIN' + arr.toString();
query;

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

@Preethi26 

add this in variable attributes

ref_qual_elements=group;action

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

13 REPLIES 13

dhanu3
Kilo Sage

@Preethi26 add reference qualifier and pass current selected group. you have to use script include to check existing roles for that group and pass list of roles which are not present.

Anand Kumar P
Giga Patron
Giga Patron

Hi @Preethi26 ,

You can use below script i have tried in my pdi
I am hard coding the sys_id of group so you have to pass group sys_id  from reference qualifier to script include and return roles from script include like "sys_id IN" + rolesToExclude.join(',');

 

var selectedGroup = '075911922ffe751056a7c786f699b62b';
var usersInGroup = new GlideRecord('sys_user_grmember');
usersInGroup.addQuery('group', selectedGroup);
usersInGroup.query();
var userIDs = [];
while (usersInGroup.next()) {
    userIDs.push(usersInGroup.user.toString());
}
var rolesAssigned = new GlideRecord('sys_user_has_role');
rolesAssigned.addQuery('user', 'IN', userIDs);
rolesAssigned.query();
var rolesToExclude = [];
while (rolesAssigned.next()) {
    rolesToExclude.push(rolesAssigned.role.getDisplayValue());
}

gs.print(rolesToExclude);

 

AnandKumarP_0-1701416488784.png

 

AnandKumarP_1-1701416524815.png

Please mark it as helpful and solution proposed if it serves your purpose.

Thanks,

Anand

 

Hi @Anand Kumar P  Thank You.

Guess this script will return roles of selected user..

My doubt is... if in catalog item we have 2 variables. "Group" and "Roles to be Added"

So if group AAA has itil role already in it,

In "roles to be added" variable  itil role should not be visible for selection.

Hi @Preethi26 ,

Above script i am passing group sysid and getting users in that group pushing it to array and getting all users roles.

In your case pass group sysid from reference qualifier and in script include use script i have added get all users roles in that group while returning to reference qualifier you can use sysid not in that roles so you will get roles not in that group and show in list collector.

Thanks,

Anand