- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2025 03:33 AM
Hello All,
why is my below code not working, Just wanted to check if the user is the member of one of these groups
Script include :-
Client script :-
if (g_form.getValue("request_type") == 'DB') {
var user_group_ga = new GlideAjax('x_novrp_it.NVS_DBAccess_util');
user_group_ga.addParam('sysparm_name', 'checkUserGroup');
user_group_ga.addParam('sysparm_userid', g_user.userName);
user_group_ga.addParam('sysparm_groupname', 'GROUP1');
user_group_ga.addParam('sysparm_groupname2', 'GROUP2');
user_group_ga.getXML(checkUserGroup);
function checkUserGroup(response) {
var result = response.responseXML.documentElement.getAttribute("answer");
if (result == 'false') {
g_form.clearValue("request_type");
alert("can't be submitted.");
}
}
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2025 05:51 AM
Hi @learnerJohns - What if you try like this?
checkUserGroup: function() {
var user_id = this.getParameter("sysparm_userid");
var group_names = this.getParameter("sysparm_groupnames"); // Comma-separated list of group names
if (!user_id || !group_names) {
return 'false'; // Return false if input is invalid
}
// Split the group names into an array
var groups = group_names.split(',');
// Check if the user belongs to any of the groups
var user = gs.getUser().getUserByID(user_id);
if (user) {
for (var i = 0; i < groups.length; i++) {
if (user.isMemberOf(groups[i].trim())) {
return 'true';
}
}
}
return 'false';
}
Note: I switched group names to comma-separated list for scalability
if (g_form.getValue("request_type") == 'DB') {
var user_group_ga = new GlideAjax('x_novrp_it.NVS_DBAccess_util');
user_group_ga.addParam('sysparm_name', 'checkUserGroup');
user_group_ga.addParam('sysparm_userid', g_user.userID); // Use user ID
// Pass the group names as a JSON array
var groupNamesArray = ['GROUP1', 'GROUP2', 'GROUP3', 'GROUP4'];
user_group_ga.addParam('sysparm_groupnames', JSON.stringify(groupNamesArray));
user_group_ga.getXMLAnswer(function(response) {
var result = response; // Get the server's response as a string
if (result == 'false') {
g_form.clearValue("request_type");
alert("Can't be submitted.");
}
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2025 05:51 AM
Hi @learnerJohns - What if you try like this?
checkUserGroup: function() {
var user_id = this.getParameter("sysparm_userid");
var group_names = this.getParameter("sysparm_groupnames"); // Comma-separated list of group names
if (!user_id || !group_names) {
return 'false'; // Return false if input is invalid
}
// Split the group names into an array
var groups = group_names.split(',');
// Check if the user belongs to any of the groups
var user = gs.getUser().getUserByID(user_id);
if (user) {
for (var i = 0; i < groups.length; i++) {
if (user.isMemberOf(groups[i].trim())) {
return 'true';
}
}
}
return 'false';
}
Note: I switched group names to comma-separated list for scalability
if (g_form.getValue("request_type") == 'DB') {
var user_group_ga = new GlideAjax('x_novrp_it.NVS_DBAccess_util');
user_group_ga.addParam('sysparm_name', 'checkUserGroup');
user_group_ga.addParam('sysparm_userid', g_user.userID); // Use user ID
// Pass the group names as a JSON array
var groupNamesArray = ['GROUP1', 'GROUP2', 'GROUP3', 'GROUP4'];
user_group_ga.addParam('sysparm_groupnames', JSON.stringify(groupNamesArray));
user_group_ga.getXMLAnswer(function(response) {
var result = response; // Get the server's response as a string
if (result == 'false') {
g_form.clearValue("request_type");
alert("Can't be submitted.");
}
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2025 06:42 AM - edited 01-24-2025 06:43 AM
Your script is correct please return Boolean values in string.
return "true" - like this