Assignmnet group Name to populate in List Collector

Arun_Manoj
Mega Sage

Hi,

 

I need to populate the list of assignment group belongs to the Requested for field.

eg:

scenario 1:

If the requested for user not the logged in user, then select the requested for and the group list collector needs to display the list of the groups the requested for belongs too.

scenario 2:
Another , list collector needs to remove group name from the list collector which belongs to the requested for user.

 

 

Please suggest a feasible solution to do in catalog item form.

2 REPLIES 2

KrishanSharma
Tera Expert

Hi,

I'm assuming scenario 1 will work if logged in user does not match with requested for user, else the scenario 2 will work. So to handle this scenario you can do something like this:


On Change Client Script on Requested For field/variable:

 

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {
        return;
    }

    var userID = g_user.userID;
    var list = g_form.getValue('YOUR_LIST_VARIABLE_NAME');

    var ga = new GlideAjax('CatalogAjaxUtils');
    ga.addParam('sysparm_name', 'getUserGroups');
    ga.addParam('sysparm_user_id', userID);
    ga.getXML(updateList);

    function updateList(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer) {
if (g_form.getValue('REQUESTED_FOR_VARIABLE_NAME') != userID) {
                //Scenario 1: Add Requested For user's group in current list, if logged in user is same
                list += ',' + answer;
                var combinedList = list.split(',');
                var arrayUtil = new ArrayUtil();
                var arr1 = arrayUtil.unique(combinedList);

                g_form.setValue('YOUR_LIST_VARIABLE_NAME', arr1);
            } else {
                //Scenario 2: Remove Requested For user's group from current list
                var groupsToBeRemovedArr = answer.split(',');
                var arr2 = arr2.filter(function(val) {
                    return (groupsToBeRemovedArr.indexOf(val) == -1 ? true : false);
                });

                g_form.setValue('YOUR_LIST_VARIABLE_NAME', arr2);
            }
        }
    }
}

 

 

Client Callable Script Include:

 

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

    getUserGroups: function() {
        var userID = this.getParameter('sysparm_user_id');
        var groups = [];

        var memberGr = new GlideRecord('sys_user_grmember');
        memberGr.addQuery('user', userID);
        memberGr.query();
        while (memberGr.next()) {
            groups.push(memberGr.group.toString());
        }
        return groups.toString();
    },
    type: 'CatalogAjaxUtils'
});

 

 

Note: I just wrote these scripts without doing testing from my end to confirm, so if you face any issues with this, please let me know. Will try to debug.

If you think my answer resolved your query, please mark it helpful & complete 🙂

Moin Kazi
Kilo Sage
Kilo Sage

Hi @Arun_Manoj ,

 

In your scenario, when the user selects a "Requested For," the associated group names will populate in the "Groups" list collector field. If the "Requested For" field is cleared, the group names will also be removed from the list collector.

Here's an onChange Client Script for the Requested For field:

 

 

function onChange(control, oldValue, newValue, isLoading) {
    var ga = new GlideAjax('GroupList');
    ga.addParam('sysparm_name', 'getGroupList');
    ga.addParam('sysparm_id', newValue);
    ga.getXMLAnswer(getGroups);

    function getGroups(answer) {
        g_form.setValue('groups', answer);
    }

}

 

 

Here's a client-callable Script Include to fetch group names associated with the Requested For user. This Script Include will return the group sys_ids, which can then be used to populate the "Groups" list collector field in your client script.

Script Include Name: GroupList

Client callable:   Checked
 

 

 

var GroupList = Class.create();
GroupList.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getGroupList: function() {
        var arr = '';
        var userid = this.getParameter('sysparm_id');
        var grp = new GlideRecord('sys_user_grmember');
        grp.addQuery('user', userid);
        grp.query();
        while (grp.next()) {
            arr +=grp.group.toString() + ",";
        }
        return arr;
    },
    type: 'GroupList'
});

 

 

 

Output :

MoinKazi_0-1730275573210.png

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you found my response **helpful**, I’d appreciate it if you could take a moment to select **"Accept as Solution"** and **"Helpful"** Your support not only benefits me but also enriches the community.

 

Thank you!
Moin Kazi
www.linkedin.com/in/moinuddinkazi

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~