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

anshul_goyal
Kilo Sage

Hello @Amrit4,

Which user do you want to remove (Requested For or is there any other field that belongs to the user table)? And what is the purpose of the "I want to" field in the catalog?

Thanks
Anshul

Hi @anshul_goyal 

I want to field has other options as well like 'add user'. But we are now concerned with only remove user option.

I want to remove the requested_for user.

Ankur Bawiskar
Tera Patron
Tera Patron

@Amrit4 

you can use onChange on that list collector and use GlideAjax to check number of members

what did you start with and where are you stuck?

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

hi @Ankur Bawiskar 

i am working on onChange and GlideAjax. I will update you with the scripts in sometime. meanwhile if you have a script related please share.