- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2023 11:27 AM
I have two reference fields i.e., user(list collector) and Group (reference).
When users are selected in list collector, I have to check whether user is lareday part of the group or not. If user is already part of the group means I have to show error message and form should not get submitted.
For this I have written a on submit client script.
Here is my on submit client script
function onSubmit() {
if (g_scratchpad.isFormValid)
return true;
//Type appropriate comment here, and begin script below
var getUsers = g_form.getValue('ioc_select_group_name'); //list of users from list collector
var getGroup = g_form.getValue('ioc_select_group'); //group reference field
var gra = new GlideAjax('global.IOC_CheckOnboradingDate');
gra.addParam('sysparm_name', 'checkDuplicateSubmit');
gra.addParam('sysparm_groupname', getGroup);
gra.addParam('sysparm_duplicates', getUsers);
gra.getXMLAnswer(getResponse);
return false;
function getResponse(answer) {
if (answer == 'true') {
g_form.addErrorMessage('Some users are already part of this group');
return false;
}
var actionName = g_form.getActionName();
g_scratchpad.isFormValid = true;
g_form.submit(actionName);
}
}
This is my script include function:
checkDuplicateSubmit: function() {
var usrDupSubmit = this.getParameter('sysparm_duplicates'); //users list
var userGroupSubmit = this.getParameter('sysparm_groupname');
gs.info("users sysid's= "+ usrDupSubmit);
var arr = usrDupSubmit.split(',');
for (i = 0; i < arr.length; i++) {
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', arr[i]);
gr.addQuery('group', userGroupSubmit);
gr.query();
if(gr.next()){
return true;
}else{
return false;
}
}
},
I'm not sure how to validate this on sscript include. Please help me on this.
Thank you.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2023 11:42 AM
Hello,
Seems the script include will return the first user itself. So i would suggest to use the script as below:-
checkDuplicateSubmit: function() {
var usrDupSubmit = this.getParameter('sysparm_duplicates'); //users list
var userGroupSubmit = this.getParameter('sysparm_groupname');
gs.info("users sysid's= "+ usrDupSubmit);
var flag;
var arr = usrDupSubmit.split(',');
for (i = 0; i < arr.length; i++) {
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', arr[i]);
gr.addQuery('group', userGroupSubmit);
gr.query();
if(gr.next()){
flag= 'true';
break;
}else{
falg= 'false';
}
}
if (flag=='true')
{
return true;
}
else
{
return false;
}
},
Also apply some logs just to make sure everything is working as expected.
Please mark my answer as correct based on Impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2023 11:37 AM
Hi,
I would recommend another approach. Add a reference qualifier that excludes the existing members of the group. That way you don't have to bother with the check and abort functionality. The user experience will be better without error messages and aborts.
Regards,
Niklas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2023 11:42 AM
Hello,
Seems the script include will return the first user itself. So i would suggest to use the script as below:-
checkDuplicateSubmit: function() {
var usrDupSubmit = this.getParameter('sysparm_duplicates'); //users list
var userGroupSubmit = this.getParameter('sysparm_groupname');
gs.info("users sysid's= "+ usrDupSubmit);
var flag;
var arr = usrDupSubmit.split(',');
for (i = 0; i < arr.length; i++) {
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', arr[i]);
gr.addQuery('group', userGroupSubmit);
gr.query();
if(gr.next()){
flag= 'true';
break;
}else{
falg= 'false';
}
}
if (flag=='true')
{
return true;
}
else
{
return false;
}
},
Also apply some logs just to make sure everything is working as expected.
Please mark my answer as correct based on Impact.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2023 12:27 PM
Hi @Naga Ravindra R ,
By looking at the script - logic looks ok, whats the issue??
Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2023 12:49 PM
please check with below script.
function onSubmit() {
var userField = g_form.getControl('your_user_list_collector_field'); // Replace with the actual field name
var groupField = g_form.getControl('your_group_reference_field'); // Replace with the actual field name
var selectedUsers = userField.getReference(); // Get the selected users from the list collector
var groupId = groupField.getValue(); // Get the referenced group ID
// Check if the selected users are already part of the group
var userAlreadyInGroup = false;
if (selectedUsers && selectedUsers.length > 0 && groupId) {
for (var i = 0; i < selectedUsers.length; i++) {
var userSysID = selectedUsers[i].value;
// Check if the user is part of the group
var userInGroup = isUserInGroup(userSysID, groupId);
if (userInGroup) {
userAlreadyInGroup = true;
break;
}
}
}
// Display an error message if a user is already in the group
if (userAlreadyInGroup) {
g_form.addErrorMessage("One or more selected users are already part of the group. Please remove them before submitting the form.");
return false; // Prevent the form from being submitted
}
return true; // Allow the form to be submitted
}
// Function to check if a user is part of the group
function isUserInGroup(userSysID, groupSysID) {
// Query the User [sys_user] table to check if the user is in the group
var userGroup = new GlideRecord('sys_user_grmember');
userGroup.addQuery('user', userSysID);
userGroup.addQuery('group', groupSysID);
userGroup.query();
return userGroup.hasNext();
}
Please replace 'your_user_list_collector_field' and 'your_group_reference_field' with the actual field names in your instance. Also, ensure that the script is attached to the appropriate catalog item or form.