client script to check if a group already exists

Rick Larson
Tera Contributor

Hello, I'm creating a catalog for creating groups, but I would like to check if the name of the group that is being requested already exists, I created a script but on the client I can't use "current.variables" I would like some help.

here is my script

var gr = new GlideRecord("sys_user_group");
gr.addQuery("name",group);
gr.query();

if(!gr.next()){
//Group not available..
gr.initialize();
gr.name=group;
gr.insert();
}else{
//Group Available..
}

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Following Client Script and Script Include will check if a group exists.

Client Script. I have a onChange() script on single line variable accepting group name to check.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    try {
        var ajax = new GlideAjax('GroupUtil');
        ajax.addParam('sysparm_name', 'existsGroup');
        ajax.addParam('sysparm_group_name', newValue);
        ajax.getXMLAnswer(function(answer) {
            if (answer.length > 0) {
                g_form.setValue('reply', answer);
            }
        });
    } catch (e) {
        alert(e.message);
    }
}

Script Include:

var GroupUtil = Class.create();
GroupUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    existsGroup: function() {
        var groupName = this.getParameter('sysparm_group_name');
        var grGroup = new GlideRecord('sys_user_group');
        if (grGroup.get('name', groupName)) {
            return true;
        }
        return false;
    },
    type: 'GroupUtil'
});

find_real_file.png

Execution result:

1. When group exists, Reply is set to "true".

find_real_file.png

2. When group does not exist, Reply is set to "false”。

find_real_file.png

View solution in original post

5 REPLIES 5

Thanks for the help, it worked! I just have one more doubt, I would like that instead of a field it was just an error message, but I can't do the validation, I tried to put in the "IF" answer == true but it doesn't work, how would it be?