Populate groups and roles based on selected user

Kai Tingey
Tera Guru

Hi All

 

I have a form where I would like to populate 2 reference variables - one with all the roles a user has, and one with all the groups a user is a member of. The wrinkle is that the list of groups / roles needs to be based off another reference field where you select the user, and not the current logged in user (so gsGetUser isn't helping me).

 

it's an access add / remove form that is to go on our service portal. Add is easy enough, but they want the 'remove' fields to only show what the selected user currently has.

 

requested for user : (sys_user reference field)

groups to remove from : (groups selected user is a member of)

roles to remove : (roles selected user has)

 

i've been trying various reference qualifiers and glide ajax and getting nowhere fast. I'm trying to avoid making table changes (e.g. display value) if there is another way around it - any advice would be appreciated.

1 ACCEPTED SOLUTION

AnveshKumar M
Tera Sage
Tera Sage

Hi @Kai Tingey ,

You can use Client Callable Script include and Glide Ajax in Catalog Client Script.

 

Variables in Catalog Form:

  1. requested for user [requested_for_user] : reference field reffering sys_user table
  2. groups to remove from [groups_to_remove_from]: list collector field referring to sys_user_group table 
  3. roles to remove [roles_to_remove]: list collector field referring to sys_user_role table

Client Callable Script Include:

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

    getRoles: function() {
        var uid = this.getParameter('sysparm_uid');
        var relGr = new GlideRecord('sys_user_has_role');
		relGr.addEncodedQuery('user='+ uid +'^inherited=false');
        relGr.query();
        var roles = '';
		var roles_disp = '';
        while (relGr.next()) {
            roles = roles + ',' + relGr.getValue('role');
            roles_disp = roles_disp + ',' + relGr.getDisplayValue('role');
        }
        roles = roles.replace(/(^,)|(,$)/g, "");
		roles_disp = roles_disp.replace(/(^,)|(,$)/g, "");
        var json_data = {
            'roles': roles,
			'roles_disp': roles_disp
        };
        return JSON.stringify(json_data);
    },

    getGroups: function() {
        var uid = this.getParameter('sysparm_uid');
        var groupGr = new GlideRecord('sys_user_grmember');
	    groupGr.addQuery('user', uid );
        groupGr.query();
        var groups = '';
        var groups_disp = '';
        while (groupGr.next()) {
            groups = groups + ',' + groupGr.getValue('group');
            groups_disp = groups_disp + ',' + groupGr.getDisplayValue('group');
        }
        groups = groups.replace(/(^,)|(,$)/g, "");
        groups_disp = groups_disp.replace(/(^,)|(,$)/g, "");
        var json_data = {
            'groups': groups,
            'groups_disp': groups_disp
        };
        return JSON.stringify(json_data);
    },	

    type: 'UserDataUtils'
});

 

onChange Catalog Client Script for variable requested for user:

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

    var parentGa1 = new GlideAjax('UserDataUtils');
    parentGa1.addParam('sysparm_name', 'getRoles');
    parentGa1.addParam('sysparm_uid', g_form.getValue("requested_for_user"));
    parentGa1.getXMLAnswer(updateUserRoles);

    function updateUserRoles(answer) {
        if (answer) {
            var resp = JSON.parse(answer);
            if (resp['roles'].length >= 32) {
                g_form.setValue("roles_to_remove", '');
                if (window === null) {
                    g_form.setValue("roles_to_remove", resp['roles'], resp['roles_disp']);
                } else {
                    addItemstoList('roles_to_remove', resp['roles'], resp['roles_disp']); // Native Compatible
                }
            }
        }
    }

    var parentGa2 = new GlideAjax('UserDataUtils');
    parentGa2.addParam('sysparm_name', 'getGroups');
    parentGa2.addParam('sysparm_uid', g_form.getValue("requested_for_user"));
    parentGa2.getXMLAnswer(updateUserGroups);

    function updateUserGroups(answer) {
        if (answer) {
            var resp = JSON.parse(answer);
            if (resp['groups'].length >= 32) {
                g_form.setValue("groups_to_remove_from", '');
                if (window === null) {
                    g_form.setValue("groups_to_remove_from", resp['groups'], resp['groups_disp']);
                } else {
                    addItemstoList('groups_to_remove_from', resp['groups'], resp['groups_disp']); // Native Compatible
                }
            }
        }
    }

    function addItemstoList(listCollector, sys_ids, disp_values) {

        var arrSysId = sys_ids.split(',');
        var arrName = disp_values.split(',');

        var varName = listCollector;
        var leftBucket = gel(varName + '_select_0');
        var rightBucket = gel(varName + '_select_1');
        var rightOptions = rightBucket.options;
        var rightIDs = [];

        //Remove --None--
        for (var k = 0; k < rightOptions.length; k++) {
            var value = rightOptions[k].innerHTML;
            if (value == '--None--') {
                rightBucket.remove(0);
            }
        }

        // Add new options
        if (arrName.length > 0) {
            var myCIArray = arrName.toString().split(',');
            for (var j = 0; j < myCIArray.length; j++) {
                addOption(rightBucket, arrSysId[j], myCIArray[j]);
                sortSelect(rightBucket);
                leftBucket.onchange();
            }
        }

        // sort the buckets
        sortSelect(rightBucket);
    }

}

 

Note: Only the roles which are not inherited will be fetched and it should be the desired one.

 

The groups_to_remove_from and roles_to_remove catalog variables will contain the sys_ids of the roles and groups which you can use for backend processing.

 

Thanks,

Anvesh

 

Thanks,
Anvesh

View solution in original post

2 REPLIES 2

AnveshKumar M
Tera Sage
Tera Sage

Hi @Kai Tingey ,

You can use Client Callable Script include and Glide Ajax in Catalog Client Script.

 

Variables in Catalog Form:

  1. requested for user [requested_for_user] : reference field reffering sys_user table
  2. groups to remove from [groups_to_remove_from]: list collector field referring to sys_user_group table 
  3. roles to remove [roles_to_remove]: list collector field referring to sys_user_role table

Client Callable Script Include:

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

    getRoles: function() {
        var uid = this.getParameter('sysparm_uid');
        var relGr = new GlideRecord('sys_user_has_role');
		relGr.addEncodedQuery('user='+ uid +'^inherited=false');
        relGr.query();
        var roles = '';
		var roles_disp = '';
        while (relGr.next()) {
            roles = roles + ',' + relGr.getValue('role');
            roles_disp = roles_disp + ',' + relGr.getDisplayValue('role');
        }
        roles = roles.replace(/(^,)|(,$)/g, "");
		roles_disp = roles_disp.replace(/(^,)|(,$)/g, "");
        var json_data = {
            'roles': roles,
			'roles_disp': roles_disp
        };
        return JSON.stringify(json_data);
    },

    getGroups: function() {
        var uid = this.getParameter('sysparm_uid');
        var groupGr = new GlideRecord('sys_user_grmember');
	    groupGr.addQuery('user', uid );
        groupGr.query();
        var groups = '';
        var groups_disp = '';
        while (groupGr.next()) {
            groups = groups + ',' + groupGr.getValue('group');
            groups_disp = groups_disp + ',' + groupGr.getDisplayValue('group');
        }
        groups = groups.replace(/(^,)|(,$)/g, "");
        groups_disp = groups_disp.replace(/(^,)|(,$)/g, "");
        var json_data = {
            'groups': groups,
            'groups_disp': groups_disp
        };
        return JSON.stringify(json_data);
    },	

    type: 'UserDataUtils'
});

 

onChange Catalog Client Script for variable requested for user:

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

    var parentGa1 = new GlideAjax('UserDataUtils');
    parentGa1.addParam('sysparm_name', 'getRoles');
    parentGa1.addParam('sysparm_uid', g_form.getValue("requested_for_user"));
    parentGa1.getXMLAnswer(updateUserRoles);

    function updateUserRoles(answer) {
        if (answer) {
            var resp = JSON.parse(answer);
            if (resp['roles'].length >= 32) {
                g_form.setValue("roles_to_remove", '');
                if (window === null) {
                    g_form.setValue("roles_to_remove", resp['roles'], resp['roles_disp']);
                } else {
                    addItemstoList('roles_to_remove', resp['roles'], resp['roles_disp']); // Native Compatible
                }
            }
        }
    }

    var parentGa2 = new GlideAjax('UserDataUtils');
    parentGa2.addParam('sysparm_name', 'getGroups');
    parentGa2.addParam('sysparm_uid', g_form.getValue("requested_for_user"));
    parentGa2.getXMLAnswer(updateUserGroups);

    function updateUserGroups(answer) {
        if (answer) {
            var resp = JSON.parse(answer);
            if (resp['groups'].length >= 32) {
                g_form.setValue("groups_to_remove_from", '');
                if (window === null) {
                    g_form.setValue("groups_to_remove_from", resp['groups'], resp['groups_disp']);
                } else {
                    addItemstoList('groups_to_remove_from', resp['groups'], resp['groups_disp']); // Native Compatible
                }
            }
        }
    }

    function addItemstoList(listCollector, sys_ids, disp_values) {

        var arrSysId = sys_ids.split(',');
        var arrName = disp_values.split(',');

        var varName = listCollector;
        var leftBucket = gel(varName + '_select_0');
        var rightBucket = gel(varName + '_select_1');
        var rightOptions = rightBucket.options;
        var rightIDs = [];

        //Remove --None--
        for (var k = 0; k < rightOptions.length; k++) {
            var value = rightOptions[k].innerHTML;
            if (value == '--None--') {
                rightBucket.remove(0);
            }
        }

        // Add new options
        if (arrName.length > 0) {
            var myCIArray = arrName.toString().split(',');
            for (var j = 0; j < myCIArray.length; j++) {
                addOption(rightBucket, arrSysId[j], myCIArray[j]);
                sortSelect(rightBucket);
                leftBucket.onchange();
            }
        }

        // sort the buckets
        sortSelect(rightBucket);
    }

}

 

Note: Only the roles which are not inherited will be fetched and it should be the desired one.

 

The groups_to_remove_from and roles_to_remove catalog variables will contain the sys_ids of the roles and groups which you can use for backend processing.

 

Thanks,

Anvesh

 

Thanks,
Anvesh

Kai Tingey
Tera Guru

Thankyou so much Anvesh. That's a good solution. It doesn't limit the list, but by populating it with all their groups and then making them remove the ones that need to stay - I think I can sell that.

 

I had been trying to use sys_user_has_role and sys_user_grmember to limit the available selections, but the display values are not friendly to the form and I am hesitant to make table changes if there is another way.