Set values dynamically to a list collector catalog variable(Groups)

Sarah Bouil
Tera Expert

In Catalog form I have a choice field 'Access' with 2 drop down values 'Admin' and 'Non-Admin' and other field is 'Groups'(type is: list collector). If I select 'Admin' as 'Access' then I should see only groups(I mean admin groups) that contains 'admin' role and if I select 'Non-Admin' as 'Access' then I should see only groups(Non admin groups) that does not contains 'admin' role. I tried with below code but it is not working.

 

Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

//Type appropriate comment here, and begin script below
var ga = new GlideAjax('Cat_Var_GetGrps'); //calling script include

if (g_form.getValue('access_user') == 'non_admin') { //access_user- is the drop down variable
ga.addParam('sysparm_name', 'getUserGroups'); //Script include function
} else {
ga.addParam('sysparm_name', 'getUserGroupsAdmin'); //Script include function
}
ga.addParam('sysparm_user', newValue); //On change Value
ga.getXML(Cat_Var_GetGrps);

function Cat_Var_GetGrps(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var filStr = answer;
setMyFilter(answer);
function setMyFilter(filStr) {
eval(groups_dis+'g_filter.reset()'); // groups_dis- is the list collector varaible
eval(groups_dis+'g_filter.setQuery(filStr)');
eval(groups_dis+'acRequest(null)');
}

}
}

 

Script Include:

var Cat_Var_GetGrps = Class.create();
Cat_Var_GetGrps.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getUserGroupsAdmin: function() {
function groupHasAdminRole(group) {
var hasadmin = false;
var grAdminRole = new GlideRecord('sys_group_has_role');
grAdminRole.addQuery('group.name', group);
grAdminRole.addQuery('role.name', 'CONTAINS', 'admin');
grAdminRole.query();

if (grAdminRole.next()) {
hasadmin = true;
}
return hasadmin;
}

var dataArr = [];
var data = {};
var user = this.getParameter('sysparm_user');
var grGroup = new GlideRecord('sys_user_grmember');
grGroup.addQuery('user', this.getParameter('sysparm_user'));
grGroup.addQuery('active', true);
grGroup.query();
while (grGroup.next()) {
var resultAdmin = groupHasAdminRole(grGroup.group.name.getValue());

if (resultAdmin) {
data = {};
data.user = grGroup.getValue('user');
data.group = grGroup.getValue('group');
dataArr.push(data);
}
}

return JSON.stringify(dataArr);

},
getUserGroups: function() {
function groupHasAdminRole(group) {
var hasadmin = false;
var grAdminRole = new GlideRecord('sys_group_has_role');
grAdminRole.addQuery('group.name', group);
grAdminRole.addQuery('role.name', 'CONTAINS', 'admin');
grAdminRole.query();

if (grAdminRole.next()) {
hasadmin = true;
}
return hasadmin;
}

var dataArr = [];
var data = {};
var user = this.getParameter('sysparm_user');
var grGroup = new GlideRecord('sys_user_grmember');
grGroup.addQuery('user', this.getParameter('sysparm_user'));
grGroup.addQuery('active', true);
grGroup.query();
while (grGroup.next()) {
var resultAdmin = groupHasAdminRole(grGroup.group.name.getValue());

if (!resultAdmin) {
data = {};
data.user = grGroup.getValue('user');
data.group = grGroup.getValue('group');
dataArr.push(data);
}
}

return JSON.stringify(dataArr);

},

 

type: 'Cat_Var_GetGrps'
});

 

Kindly assist me what was wrong with my script?

 

10 REPLIES 10

Karunakaran
Mega Guru

Hi @Sarah Bouil ,

 

Create two different list fields and hide one question based on the selection of access type. For this you can use the Catalogue UI Policy.

 

Example:

1. Admin Groups list for Admin Access

2. Non Admin Groups list for Non-Admin Access

 

You can configure the groups in the reference table configuration. No need of scripts.

 

Hope this helps.

 

Thank you.

 

Regards,

Karunakaran.

Thank you for your quick reply. I understand I will create 2 different list collector type(reference to: sys_user_groups table) but my question is how to configure to display Admin groups for admin access and non admin groups for non-admin access by using catalog ui policy?

Hi Again @Sarah Bouil 

 

filter the group with roles field

For admin role:

Karunakaran_0-1713956612398.png

 

For Non-admin role:

 

Karunakaran_1-1713956661072.png

 

Please let me know, if this is not working.

 

Thank you.

 

Taking this route will just mean two different reference qualifiers on the variables you create. One showing the admins, one showing the non admins.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark