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

swathisarang98
Giga Sage
Giga Sage

Hi @Sarah Bouil,

 

You can create reference qualifier on Group field and call the script include, in script include check whether the access selected is admin or non admin then based on choice return the value.

 

Note:the Group field i have used here is of type reference and referencing to sys_user_group.

 

I have tried in my pdi the below code works please give it a try,

 

Reference qualifier:

swathisarang98_0-1713969131640.png

 

 

 

javascript: new  checkRole().getRole(current.variables.access);

 

 

 

 

Script Include:

swathisarang98_1-1713969198045.png

 

 

 

var checkRole = Class.create();
checkRole.prototype = {
    initialize: function() {},
    
getRole: function(choice) {

var groups = [];
if (choice == 'admin') {
              var adminGroup = new GlideRecord('sys_group_has_role');
              adminGroup.addQuery('role','2831a114c611228501d4ea6c309d626d'); // role 'admin' sys_id, checking whether role is admin
               adminGroup.query();
               while (adminGroup.next()) {
               groups.push(adminGroup.group.toString());
            }
               return 'sys_idIN' + groups;
        }

else if (choice == 'non_admin'){	
            var nonAdminGroup = new GlideRecord('sys_group_has_role');
            nonAdminGroup.addEncodedQuery('role!=2831a114c611228501d4ea6c309d626d^ORrole=NULL'); //'admin' sys_id, checking whether role is notadmin
            nonAdminGroup.query();
            while (nonAdminGroup.next()) {		
                groups.push(nonAdminGroup.group.toString());
            }
            return 'sys_idIN' + groups;
        
		}

    },

    type: 'checkRole'
};

 

 

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang