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

Sukraj Raikhraj
Kilo Sage

https://community.servicenow.com/community?id=community_question&sys_id=028c8603db51770cfece0b55ca961918

 

Hey, see if this link helps. 

Community Alums
Not applicable

In client script, You should only be validating if  group with same name already exist in the system. Creation of ServiceNow group, should be done in workflow.

Just FYI, using GlideRecord query in client script is not best practice. 

You can validate existence of the group using below :

var gr = new GlideRecord("sys_user_group");
gr.addQuery("name",g_form.getValue("<your_group_field_name>");
gr.query();

if(gr.next()){

g_form.addErrorMessage("Group with same name already exist");




}

 

Thank you very much for the answer, yes I agree there in the script, thanks even just to validate if the group exists in the client script, a question how would it be if I were to make this call through an ajax script?

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