
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2023 11:09 PM - edited 09-27-2023 11:14 PM
Hi all,
Need help with a requirement.
We have a catalog which has a functionality to remove users from a selected group.
There are 3 varaibles, Requested for, I want to, and 'Which group access do you wish to remove?'.
Which group access do you wish to remove? is a list collector which shows the value of group for which the requested for user is a member.
Now my requirement is that if i request a particular user has to be removed from a particular group or groups, the calatog should validate if any of the selected group has members less than 2. If there is any group with less than 2 members it should throw error and restrict submission of form.
Please help me with the required script to achieve this. Thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2023 09:15 AM
Hi @Amrit4
The form is still submitted due to the Asynchronous GlideAjax call.
Let's try the below onSubmit Client Script.
function onSubmit() {
/*** Pop this gem into your script! */
if (g_scratchpad.isFormValid) {
return true;
}
var actionName = g_form.getActionName();
var eachgroup = g_form.getValue('which_technical_service_ts_group_access_do_you_wish_to_remove');
var ga = new GlideAjax('GroupMembershipUtils');
ga.addParam('sysparm_name', 'getUserDetails');
ga.addParam('sysparm_userid', eachgroup);
ga.getXMLAnswer(getResponse);
function getResponse(response) {
var res = JSON.parse(response);
if (res.isLessThan2 == 'true') {
alert('You cannot remove the user from ' + res.group_name);
return false;
} else {
alert('you can submit');
/*** Pop this gem into your script! */
g_scratchpad.isFormValid = true;
g_form.submit(actionName);
}
}
return false;
}
Let me know if it works for you
Cheers,
Tai Vu

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2023 09:40 AM - edited 10-11-2023 09:41 AM
so in case someone needs a full code for this requirement can take the below:
Client Script:
Type : OnSubmit
function onSubmit() {
if (g_scratchpad.isFormValid) {
return true;
}
var actionName = g_form.getActionName();
var eachgroup = g_form.getValue('variable_name'); //put in your list collector variable name
var ga = new GlideAjax('GroupMembershipUtils');
ga.addParam('sysparm_name', 'getUserDetails');
ga.addParam('sysparm_userid', eachgroup);
ga.getXMLAnswer(getResponse);
function getResponse(response) {
var res = JSON.parse(response);
if (res.isLessThan2 == 'true') {
alert('You cannot remove the user from ' + res.group_name);
return false;
} else {
alert('you can submit');
/*** Pop this gem into your script! */
g_scratchpad.isFormValid = true;
g_form.submit(actionName);
}
}
return false;
}
Script Include:
var GroupMembershipUtils = Class.create();
GroupMembershipUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserDetails: function() {
var eachapp = this.getParameter('sysparm_userid').toString().split(',');
var count = 0;
var obj = {};
for (var i = 0; i < eachapp.length; i++) {
var gruserRecord = new GlideRecord("sys_user_group");
gruserRecord.addQuery('sys_id', eachapp[i].toString());
gruserRecord.query();
if (gruserRecord.next()) {
var grIncOPS = new GlideRecord('sys_user_grmember');
grIncOPS.addQuery('group', gruserRecord.sys_id);
grIncOPS.query();
count = grIncOPS.getRowCount();
}
if (count < 2) {
obj.isLessThan2 = 'true';
obj.group_name = gruserRecord.name.getDisplayValue();
gs.info("answer=" + JSON.stringify(obj));
return JSON.stringify(obj);
}
}
if (count > 2) {
obj.isLessThan2 = 'false';
return JSON.stringify(obj);
}
},
type: 'GroupMembershipUtils'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2023 09:25 AM
Thanks a lot this worked

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2023 09:40 AM - edited 10-11-2023 09:41 AM
so in case someone needs a full code for this requirement can take the below:
Client Script:
Type : OnSubmit
function onSubmit() {
if (g_scratchpad.isFormValid) {
return true;
}
var actionName = g_form.getActionName();
var eachgroup = g_form.getValue('variable_name'); //put in your list collector variable name
var ga = new GlideAjax('GroupMembershipUtils');
ga.addParam('sysparm_name', 'getUserDetails');
ga.addParam('sysparm_userid', eachgroup);
ga.getXMLAnswer(getResponse);
function getResponse(response) {
var res = JSON.parse(response);
if (res.isLessThan2 == 'true') {
alert('You cannot remove the user from ' + res.group_name);
return false;
} else {
alert('you can submit');
/*** Pop this gem into your script! */
g_scratchpad.isFormValid = true;
g_form.submit(actionName);
}
}
return false;
}
Script Include:
var GroupMembershipUtils = Class.create();
GroupMembershipUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserDetails: function() {
var eachapp = this.getParameter('sysparm_userid').toString().split(',');
var count = 0;
var obj = {};
for (var i = 0; i < eachapp.length; i++) {
var gruserRecord = new GlideRecord("sys_user_group");
gruserRecord.addQuery('sys_id', eachapp[i].toString());
gruserRecord.query();
if (gruserRecord.next()) {
var grIncOPS = new GlideRecord('sys_user_grmember');
grIncOPS.addQuery('group', gruserRecord.sys_id);
grIncOPS.query();
count = grIncOPS.getRowCount();
}
if (count < 2) {
obj.isLessThan2 = 'true';
obj.group_name = gruserRecord.name.getDisplayValue();
gs.info("answer=" + JSON.stringify(obj));
return JSON.stringify(obj);
}
}
if (count > 2) {
obj.isLessThan2 = 'false';
return JSON.stringify(obj);
}
},
type: 'GroupMembershipUtils'
});