Check if Name of Supportgroup is already in Use
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hello everybody,
We have a Requirement for a Catalog Item, to enable Users to order a new support group.
The users can enter their preffered group name into a single line text field for which i would like to implement a validation, so that group names must me unique and the User receives an error message if they try to enter a group name that already exists. Can anybody assist with a catalog client script for this use case?
Kind regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
6 hours ago - last edited 6 hours ago
You can achieve this by using a Catalog Client Script and a Client Callable Script Include that checks whether the entered group name already exists in the sys_user_group table.
Below is a working example:
1.Catalog Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('CheckGroupName');
ga.addParam('sysparm_name', 'checkIfGroupExists');
ga.addParam('sysparm_group_name', newValue);
ga.getXMLAnswer(function(response) {
if (response === 'true') {
g_form.showFieldMsg('your variable name', 'This group name already exists. Please choose a different name.', 'error');
g_form.setValue('your variable name', ''); // optional: clear the field
} else {
g_form.hideFieldMsg('your variable name', true);
}
});
}
2.Script Include:
var CheckGroupName = Class.create();
CheckGroupName.prototype = {
initialize: function() {},
checkIfGroupExists: function() {
var groupName = this.getParameter('sysparm_group_name') || '';
var grp = new GlideRecord('sys_user_group');
grp.addQuery('name', groupName);
grp.query();
if (grp.next()) {
return 'true';
}
return 'false';
},
type: 'CheckGroupName'
};
— Sanjay J
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
Hi @Fab5000
you can use the below sample code:
Script Example (onChange Client Script)
-
Type: onChange
-
Variable name:
group_name -
Applies on Catalog Item
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('CheckGroupNameAJAX');
ga.addParam('sysparm_name', 'checkGroupExists');
ga.addParam('sysparm_group_name', newValue);
ga.getXMLAnswer(function(answer) {
if (answer === 'true') {
g_form.showFieldMsg('group_name', 'This group name already exists. Please choose a different one.', 'error');
g_form.setValue('group_name', '');
}
});
}
Script Include (Client Callable):
Create a new Script Include named CheckGroupNameAJAX.
var CheckGroupNameAJAX = Class.create();
CheckGroupNameAJAX.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkGroupExists: function() {
var groupName = this.getParameter('sysparm_group_name');
var gr = new GlideRecord('sys_user_group');
gr.addQuery('name', groupName);
gr.query();
return gr.hasNext() ? 'true' : 'false';
}
});If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
