Check user if he is part of one of multiple groups

Harish35
Kilo Explorer

Hi, I am trying to restrict users submitting request based on ownership. Only the users part of groups owning the server should be able to submit. here is the code i have written which is not working. this is an onchange script

var server = g_form.getReference('server',myfunc)
var user_ID = g_user.userID.split;
var servergroup = server.u_servergroups.split(,);
function myfunc(server){
var result = false;
if (server){
for(var i=0; i<servergroups.length; i++){
if(user_ID.isMemberof(servergroups[i])){
result = true;

alert(' is user part of server groups: ' + result);
}
}
}
}

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Harish,

Are you saying user will select some server and in server there is a list type of field which refers group table and if user is member of any of those group then show alert

you need to use GlideAjax and Script include for this

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
        
        var server = g_form.getReference('server').u_servergroups;

	var ga = new GlideAjax('u_userInfoAjax');
	ga.addParam('sysparm_name', "getInfo");
	ga.addParam('sysparm_server ', server );
	ga.getXMLAnswer(function(answer){
        
        if(answer.toString() == 'true')
        alert('is user part of server groups:');

	});

}

Script Include:

var u_userInfoAjax = Class.create();
u_userInfoAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getInfo: function(){
	var groups = this.getParameter('sysparm_server');
        var groupsArray = groups.toString().split(',');
        var flag = false;
        for(var i=0;i<groupsArray.length;i++){
          if(gs.getUser().isMemberOf(groupsArray[i]))
            flag = true;
        }
        return flag;
	},

	type: 'u_userInfoAjax'
});

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Hi Harish,

if those are group sys ids or group name isMemberOf() works with both

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Harish,

Any update on this?
Can you mark my answer as correct, 👍 helpful if you were able to achieve the requirement. This enables other members to learn from this thread.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader