User criteria to restrict access to a catalog item

Nico12
Mega Sage

Hi,

 

I need to restrict item to a catalog item with user criteria.

Only the members groups that have parent "Group A"  should have access.

But it seems i have an error in my script or a wrong logic.

 

checkChildGroups();

function checkChildGroups(){
	
	var arr=[];
	var grChilds = new GlideRecord('sys_user_group');
	grChilds.addEncodedQuery('parent.sys_id=e272b912db671814c84f2ded0b961985^active=true');
	grChilds.query();
	while(grChilds.next()){

		arr.push(grChilds.name.toString());

	}
	
	for(var i=0; i<arr.length; i++){

		if(gs.getUser().isMemberOf(arr[i])){

			return true;
		}
		return false;
	}
	}

 

 

Any help would be appriciated.

 

Regards,

 

1 ACCEPTED SOLUTION

AndersBGS
Tera Patron
Tera Patron

Hi @Nico12 ,

 

As far as I can read, you don't need a script in user criteria to include child groups in the user criteria. Please see community post back from 2022: Solved: Re: User Criteria group - does it include child gr... - ServiceNow Community

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

Best regards

Anders

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/

View solution in original post

4 REPLIES 4

Jaspal Singh
Mega Patron
Mega Patron

Hi Nico,

Can you try below.

checkChildGroups();

function checkChildGroups(){
	
	var arr=[];
	var grChilds = new GlideRecord('sys_user_group');
	grChilds.addEncodedQuery('parent.sys_id=e272b912db671814c84f2ded0b961985^active=true');
	grChilds.query();
	while(grChilds.next()){

		arr.push(grChilds.name.toString());

	}
	
	for(var i=0; i<arr.length; i++){

		if(gs.getUser().isMemberOf(arr[i])){

			return true;
		}
else{
		return false;
}
	}
	}

AndersBGS
Tera Patron
Tera Patron

Hi @Nico12 ,

 

As far as I can read, you don't need a script in user criteria to include child groups in the user criteria. Please see community post back from 2022: Solved: Re: User Criteria group - does it include child gr... - ServiceNow Community

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

Best regards

Anders

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/

Nico12
Mega Sage

Hi,

 

i finally succeed to make the script worked : 

My return false was not in the right place.

 

 

checkChildGroups();

function checkChildGroups(){

	var arr=[];
	var grChilds = new GlideRecord('sys_user_group');
	grChilds.addEncodedQuery('parent.sys_id=e272b912db671814c84f2ded0b961985^active=true');
	grChilds.query();
	while(grChilds.next()){

		arr.push(grChilds.name.toString());

	}

	for(var i=0; i<arr.length; i++){

		if(gs.getUser().isMemberOf(arr[i])){

			return true;

		}
	}
	return false;
}

 

 

 

but thanks @AndersBGS I didn't see this post and i will go for your solution.

 

Regards,

Great to know. Comments before were the same & thus had addded else condition in my comments.