- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2022 12:18 PM
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..
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2022 04:12 PM
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'
});
Execution result:
1. When group exists, Reply is set to "true".
2. When group does not exist, Reply is set to "false”。

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2022 12:24 PM
https://community.servicenow.com/community?id=community_question&sys_id=028c8603db51770cfece0b55ca961918
Hey, see if this link helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2022 01:51 PM
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");
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2022 01:54 PM
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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2022 04:12 PM
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'
});
Execution result:
1. When group exists, Reply is set to "true".
2. When group does not exist, Reply is set to "false”。