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

@Amrit4 

it should be an easy one. If you start it will be learning for you as well.

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

Hi @Ankur Bawiskar 

 

I have written this piece of code which seems working.

when i submit the form with less than 2 membered group, it returns error with the group name that has less than 2 members in the list collector. But the problem is when i select all group with proper members with no group less than 2 members in the list collector, it still does not submit the form. 

Could you please check my script to see where i am missing.

 

 

 

Catalog Client Script:

function onSubmit() {
var eachgroup = g_form.getValue('which_technical_service_ts_group_access_do_you_wish_to_remove');
	//alert(arr.toString());
    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);	
		} else {
		alert ('you can submit');	
		}
		
    }
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'
});

 

 

 

 

@Amrit4 
return false seems to be generic and hence whatever the count of members in the group are, its returning false and its not submitting. You change the code in client script something like this

 

 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');	
                return true;
		}
		
    }

}

 

 

Didn't work ... It returns the alert but submits the form in both the cases..

if one of the groups has less than 2 members - it returns the alert 'you cannot submit request for group_name' and submits the form (it should not submit)

if all groups have more than 2 members - it returns 'you can submit' and submits the form (this is as expected)

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