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 of user is the member of the 2 group

learnerJohns
Tera Contributor

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 :-

checkUserGroup: function() {
        var user_id = this.getParameter("sysparm_userid");
        var group_name = this.getParameter("sysparm_groupname");
        var group_name2 = this.getParameter("sysparm_groupname2");

        if (gs.getUser().getUserByID(user_id).isMemberOf(group_name) || gs.getUser().getUserByID(user_id).isMemberOf(group_name2)) {

            return true;
        } else {

            return false;
        }

 

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.");
}
}

}

}

1 ACCEPTED SOLUTION

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

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.");
        }
    });
}

 

View solution in original post

2 REPLIES 2

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

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.");
        }
    });
}

 

Roshan Tiwari
Tera Guru

Your script is correct please return Boolean values in string.

 

return "true" - like this