Validate group names in a list collector variable to check if selected group has less than 2 members

Amrit4
Tera Guru

Hi all,

 

Need help with a requirement.

We have a catalog which has a functionality to remove users from a selected group.

There are 3 varaibles, Requested for, I want to, and 'Which group access do you wish to remove?'.

Which group access do you wish to remove? is a list collector which shows the value of group for which the requested for user is a member.

Now my requirement is that if i request a particular user has to be removed from a particular group or groups, the calatog should validate if any of the selected group has members less than 2. If there is any group with less than 2 members it should throw error and restrict submission of form.

Please help me with the required script to achieve this. Thanks in advance

 

Amrit4_0-1695881010443.png

 

2 ACCEPTED SOLUTIONS

Hi @Amrit4 

The form is still submitted due to the Asynchronous GlideAjax call.

TaiVu_0-1697039591607.png

 

Let's try the below onSubmit Client Script.

function onSubmit() {

	/*** Pop this gem into your script! */
    if (g_scratchpad.isFormValid) {
        return true;
    }
    var actionName = g_form.getActionName();

    var eachgroup = g_form.getValue('which_technical_service_ts_group_access_do_you_wish_to_remove');

	var ga = new GlideAjax('GroupMembershipUtils');
    ga.addParam('sysparm_name', 'getUserDetails');
    ga.addParam('sysparm_userid', eachgroup);
    ga.getXMLAnswer(getResponse);

    function getResponse(response) {
        var res = JSON.parse(response);
        if (res.isLessThan2 == 'true') {
            alert('You cannot remove the user from ' + res.group_name);
			return false;
        } else {
            alert('you can submit');
			/*** Pop this gem into your script! */
            g_scratchpad.isFormValid = true;
            g_form.submit(actionName);
        }
    }
    return false;
}

 

Let me know if it works for you

 

Cheers,

Tai Vu

View solution in original post

Amrit4
Tera Guru

so in case someone needs a full code for this requirement can take the below:

 

 

Client Script:

Type : OnSubmit

function onSubmit() {
    if (g_scratchpad.isFormValid) {
        return true;
    }
    var actionName = g_form.getActionName();

    var eachgroup = g_form.getValue('variable_name'); //put in your list collector variable name

    var ga = new GlideAjax('GroupMembershipUtils');
    ga.addParam('sysparm_name', 'getUserDetails');
    ga.addParam('sysparm_userid', eachgroup);
    ga.getXMLAnswer(getResponse);
    function getResponse(response) {
        var res = JSON.parse(response);
        if (res.isLessThan2 == 'true') {
            alert('You cannot remove the user from ' + res.group_name);
            return false;
        } else {
            alert('you can submit');
            /*** Pop this gem into your script! */
            g_scratchpad.isFormValid = true;
            g_form.submit(actionName);
        }
    }
    return false;
}

Script Include:

var GroupMembershipUtils = Class.create();
GroupMembershipUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUserDetails: function() {
        var eachapp = this.getParameter('sysparm_userid').toString().split(',');
        var count = 0;
        var obj = {};
        for (var i = 0; i < eachapp.length; i++) {
            var gruserRecord = new GlideRecord("sys_user_group");
            gruserRecord.addQuery('sys_id', eachapp[i].toString());
            gruserRecord.query();
            if (gruserRecord.next()) {
                var grIncOPS = new GlideRecord('sys_user_grmember');
                grIncOPS.addQuery('group', gruserRecord.sys_id);
                grIncOPS.query();
                count = grIncOPS.getRowCount();
            }
            if (count < 2) {
                obj.isLessThan2 = 'true';
                obj.group_name = gruserRecord.name.getDisplayValue();
                gs.info("answer=" + JSON.stringify(obj));
                return JSON.stringify(obj);			
            }
        }
        if (count > 2) {
            obj.isLessThan2 = 'false';
            return JSON.stringify(obj);
        }
    },
    type: 'GroupMembershipUtils'
});

 

 

View solution in original post

11 REPLIES 11

Thanks a lot this worked

Amrit4
Tera Guru

so in case someone needs a full code for this requirement can take the below:

 

 

Client Script:

Type : OnSubmit

function onSubmit() {
    if (g_scratchpad.isFormValid) {
        return true;
    }
    var actionName = g_form.getActionName();

    var eachgroup = g_form.getValue('variable_name'); //put in your list collector variable name

    var ga = new GlideAjax('GroupMembershipUtils');
    ga.addParam('sysparm_name', 'getUserDetails');
    ga.addParam('sysparm_userid', eachgroup);
    ga.getXMLAnswer(getResponse);
    function getResponse(response) {
        var res = JSON.parse(response);
        if (res.isLessThan2 == 'true') {
            alert('You cannot remove the user from ' + res.group_name);
            return false;
        } else {
            alert('you can submit');
            /*** Pop this gem into your script! */
            g_scratchpad.isFormValid = true;
            g_form.submit(actionName);
        }
    }
    return false;
}

Script Include:

var GroupMembershipUtils = Class.create();
GroupMembershipUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUserDetails: function() {
        var eachapp = this.getParameter('sysparm_userid').toString().split(',');
        var count = 0;
        var obj = {};
        for (var i = 0; i < eachapp.length; i++) {
            var gruserRecord = new GlideRecord("sys_user_group");
            gruserRecord.addQuery('sys_id', eachapp[i].toString());
            gruserRecord.query();
            if (gruserRecord.next()) {
                var grIncOPS = new GlideRecord('sys_user_grmember');
                grIncOPS.addQuery('group', gruserRecord.sys_id);
                grIncOPS.query();
                count = grIncOPS.getRowCount();
            }
            if (count < 2) {
                obj.isLessThan2 = 'true';
                obj.group_name = gruserRecord.name.getDisplayValue();
                gs.info("answer=" + JSON.stringify(obj));
                return JSON.stringify(obj);			
            }
        }
        if (count > 2) {
            obj.isLessThan2 = 'false';
            return JSON.stringify(obj);
        }
    },
    type: 'GroupMembershipUtils'
});