Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Check if Name of Supportgroup is already in Use

Fab5000
Tera Contributor

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

1 ACCEPTED SOLUTION

sanjay02
Tera Guru

@Fab5000 


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'
};
If my response helped you, please click “Accept as Solution”. Your recognition motivates me to keep sharing knowledge.
— Sanjay J

View solution in original post

4 REPLIES 4

sanjay02
Tera Guru

@Fab5000 


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'
};
If my response helped you, please click “Accept as Solution”. Your recognition motivates me to keep sharing knowledge.
— Sanjay J

Fab5000
Tera Contributor

@sanjay02 

Thanks a lot!

This was very helpful.

Ravi Gaurav
Giga Sage
Giga Sage

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/

If you SupportGroup name's list is limited, get those in one array variable while Display - BR.

And then use client script to Pop-up if user types existing name, this is save multiple Server queries at user's every attempt.

Additionally if existing Supportgroup are handy browser, you can play around with it by giving suggestion to user on same time and make UI more interactive.

 

If my response & approach helped you, please click “Accept as Solution”.