Check User is member of one of the selected groups in the catalog form

Aravindk5919
Tera Contributor

Hello all,
There are two variables in the form called requested for, groups. This form is to assign the requested for to those groups. I created one onsubmit client script and script include to check whether the requested for user is already a member of any of the selected groups. if yes it need to show an error stating that the user is already the member of specific group. 
I written the following , but it was not working. Could anyone help me to fix it.
thanks,

//client script

function onSubmit() {
    // Get the Requested For user sys_id
    var requestedFor = g_form.getValue('u_requested_for');
    var selectedGroups = g_form.getValue('select_the_groups'); //list collector type     
    g_form.addInfoMessage('Selected Groups: ' + selectedGroups);
 
    // Create a GlideAjax call
    var ga = new GlideAjax('CheckUserGroupMembership');
    ga.addParam('sysparm_name', 'checkMembership');
    ga.addParam('sysparm_userId', requestedFor);
    ga.addParam('sysparm_groups', selectedGroups);
 
    // Make the asynchronous call
    ga.getXMLAnswer(checkMembershipCallback);
   
    // Callback function to handle the response
    function checkMembershipCallback(answer) {
        try {
            g_form.addInfoMessage('Server Response: ' + answer);
           
            // Check if answer is empty or undefined
            if (!answer) {
                g_form.addErrorMessage('No response received from server');
                return false;
            }
           
            var result = JSON.parse(answer);
           
            if (result.isMember) {
                g_form.addErrorMessage('Requested For user is already a member of the group - ' + result.groupName);
                return false;
            }          
            return true;           
        } catch (ex) {
            g_form.addInfoMessage('Error: ' + ex.message);
            g_form.addErrorMessage('Error checking group membership. Please contact your administrator.');
        }
    }
}



//script include
var CheckUserGroupMembership = Class.create();
CheckUserGroupMembership.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkMembership: function() {
        try {
            var userSysId = this.getParameter('sysparm_userId');
            var groupsString = this.getParameter('sysparm_groups');
           
            gs.log('CheckUserGroupMembership - userSysId: ' + userSysId,"AK");
            gs.log('CheckUserGroupMembership - groupsString: ' + groupsString,"AK");
           
            if (!userSysId || !groupsString) {
                gs.log('CheckUserGroupMembership - Missing parameters',"AK");
                return JSON.stringify({
                    isMember: false,
                    groupName: '',
                    error: 'Missing required parameters'
                });
            }
           
            var groupIds = groupsString.split(',');
            gs.log('CheckUserGroupMembership - Group IDs: ' + groupIds.join(', '),"AK");
           
            var result = {
                isMember: false,
                groupName: ''
            };
           
            var gr = new GlideRecord('sys_user_grmember');
            gr.addQuery('user', userSysId);
            gr.addQuery('group', 'IN', groupIds);
            gr.query();
           
            if (gr.next()) {
                gs.log("CheckUserGroupMembership inside if","AK");
                var groupGR = new GlideRecord('sys_user_group');
                if (groupGR.get(gr.group)) {
                    result.isMember = true;
                    result.groupName = groupGR.name.toString();
                }
            }
           
            gs.log('CheckUserGroupMembership - Result: ' + JSON.stringify(result),"AK");
            return JSON.stringify(result);
           
        } catch (ex) {
            gs.error('Error in CheckUserGroupMembership.checkMembership: ' + ex);
            return JSON.stringify({
                isMember: false,
                groupName: '',
                error: ex.toString()
            });
        }
    },

    type: 'CheckUserGroupMembership'
});
2 REPLIES 2

Toderean alexan
Tera Contributor

Hi @Aravindk5919,

  I like to know if that call to the script include is made, is here the problem? Another question, the user of catalog form should have the necessary rights to call the script include? 

Brad Bowman
Kilo Patron
Kilo Patron

Good job adding the logs.  Which ones are you seeing?  The value from the groups list collector is a comma-separated list of sys_ids, and that's what you want for the addQuery, so don't split the parameter script variable first and use it in the addQuery 

gr.addQuery('group', 'IN', groupsString);

beyond this, if you want the alert to include every group which the user is already a member of you would want to return all of the records using while (gr.next()) in place of if (gr.next()) then push the group name or whatever to an array and use this list of names in the alert.  And you can dot-walk instead of doing another GR

 var result = [];
 var gr = new GlideRecord('sys_user_grmember');
 gr.addQuery('user', userSysId);
 gr.addQuery('group', 'IN', groupsString);
 gr.query();
 while (gr.next()) {
     gs.log("CheckUserGroupMembership inside if","AK");
     result.push(gr.group.name);
 }
...
return result.join(', ');