Show a Variable if the logged In User is the Manager of the current group in catalog Item

BKSharma
Giga Contributor

Hi,

I've few variables in a catalog Item as below -

Requested By

Manager

Secondary Manager

Group Name

List of Users

 

My requirement is to show the List Control variable (List of Users), if the logged in user is the Manager of current group (Group Name). For other users this 'List of Users' variable will be invisible for other users.

 

And 2nd requirement is how I can auto populate all the list of users into this List Collector variable depended upon the Group Name variable.

3 REPLIES 3

JenniferRah
Mega Sage

For showing/hiding the List of Users variable, you will need to either write a script in a UI Policy or write a Client Script that runs when the Group Name changes.

 

For the List Collector list of users, you will need to write a scripted Reference Qualifier to look up the active users in that group and set the qualifier to those users.

Ankur Bawiskar
Tera Patron
Tera Patron

@BKSharma 

For both of your requirements, create onChange catalog client script on Group Name variable and use GlideAjax

onChange catalog client script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || !newValue) {
        g_form.setDisplay('list_of_users', false);
        g_form.clearValue('list_of_users');
        return;
    }

    var ga = new GlideAjax('GroupManagerCheckAjax');
    ga.addParam('sysparm_name', 'checkGroupManagerAndUsers');
    ga.addParam('sysparm_group_sysid', newValue);

    ga.getXMLAnswer(function(response) {
        if (!response) {
            g_form.setDisplay('list_of_users', false);
            g_form.clearValue('list_of_users');
            return;
        }

        var result = JSON.parse(response);

        if (result.isManager === true) {
            g_form.setDisplay('list_of_users', true);
            g_form.setValue('list_of_users', result.userSysIds);
        } else {
            g_form.setDisplay('list_of_users', false);
            g_form.clearValue('list_of_users');
        }
    });
}

Script Include: It should be client callable

var GroupManagerCheckAjax = Class.create();
GroupManagerCheckAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  
  checkGroupManagerAndUsers: function() {
    var userId = gs.getUserID();
    var groupSysId = this.getParameter('sysparm_group_sysid');
    var grGroup = new GlideRecord('sys_user_group');

    if (!grGroup.get(groupSysId)) {
      return JSON.stringify({ isManager: false, userSysIds: '' });
    }

    var isManager = (grGroup.getValue('manager') == userId);
    var userIds = [];

    var grMembership = new GlideRecord('sys_user_grmember');
    grMembership.addQuery('group', groupSysId);
    grMembership.query();
    while (grMembership.next()) {
      userIds.push(grMembership.getValue('user'));
    }

    return JSON.stringify({
      isManager: isManager,
      userSysIds: userIds.join(',')
    });
  }

});

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

Ankur Bawiskar
Tera Patron
Tera Patron

@BKSharma 

Would you mind closing your earlier questions by marking appropriate response as correct?

Members have invested their time and efforts in helping you.

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