Requirement is to hide assignment groups from drop down if requestor is a member of the group

sravya03
Tera Contributor

Requirement is to hide assignment groups from drop down if requestor is a member of the group in catalog item.

Example i have a ABC variable which is referenced to sys_user_group table and will have active groups in drop down.

I have a variable XYZ where we need to give user name who need access, so when person name is given on ABC drop down only group which he is not member should be visible.

ABC is a variable in multirow and XYZ is single row variable

13 REPLIES 13

Here is the query to get the list of groups you are not part of.

 

var grpMember = new GlideRecord("sys_user_grmember");
grpMember.addQuery("user", gs.getUserID());
grpMember.query();
var groupsPartOf = [];
while(grpMember.next()) {

     groupsPartOf.push(grpMember.group.toString());
}
var grGroups = new GlideRecord('sys_user_group');
grGroups.addQuery('sys_id', 'NOT IN', groupsPartOf);
grGroups.query();
var groupsNotPartOf = [];
while (grGroups.next()) {
    groupsNotPartOf.push(grGroups.name.getDisplayValue());
}
gs.info("Groups not part of: " + groupsNotPartOf.join(', '));
 
 
-Vijendra Sainy
InfoBeans

hi @Vijendra Sainy 

 

Can u pls tell whether i need to include this script in script include and call that in my reference qual

Maddysunil
Kilo Sage

@sravya03 

Client script:

 

 var requestor = g_form.getValue('XYZ'); // Get the value of the requestor field
    var ajax = new GlideAjax('MyScriptInclude'); // Create a GlideAjax object
    ajax.addParam('sysparm_name', 'getUserMembership'); // Set the server-side function to call
    ajax.addParam('requestor', requestor); // Pass the requestor's username
    ajax.getXMLAnswer(function(response) {
        var groups = response.split(','); // Parse the response to get the list of groups
        var assignmentGroupOptions = g_form.getOptions('ABC'); // Get the assignment group options
        for (var i = 0; i < assignmentGroupOptions.length; i++) {
            var groupName = assignmentGroupOptions[i].value;
            if (groups.indexOf(groupName) !== -1) {
                // Hide the assignment group option if the requestor is a member
                g_form.removeOption('ABC', groupName);
            }
        }
    }

 

Script inlclude:

 

getUserMembership: function(requestor) {
        var membership = '';
        
        // Query the sys_user_grmember table to fetch groups the user is a member of
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', 'CONTAINS', requestor); // Filter by the requestor's username
        gr.query();
        
        // Iterate through the results and concatenate group names
        while (gr.next()) {
            var groupName = gr.group.getDisplayValue(); // Get the display value of the group
            membership += groupName + ',';
        }
        
        // Remove the trailing comma
        membership = membership.slice(0, -1);
        
        return membership;
    },

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

@Maddysunil 

 

Tried the script include and catalog client script but it is not working

@sravya03 

Please apply some alert and log to debug..

Thanks