User Criteria based on Group Type

Lorenzo Stermie
Kilo Guru

Hello,
I'm trying to setup user criteria based on group type.
On a similar post someone shared this script:

var gr = new GlideRecord('sys_user_group');
gr.addActiveQuery();
gr.addQuery('name', id);
gr.setLimit(1);
gr.query();
if (gr.type == '1cb8ab9bff500200158bffffffffff62')
gr.hasNext();

But I haven't been able to make it work.
Please help

Thank you

Lorenzo

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Ah oke, now I see your functional question for the first time 🙂 Only saw scripting before.

Try below code. Queries the Group Member table. Group type should be ITIL, user should be dynamic me. If any result: true!

(function() {

	var queryStr = 'group.typeLIKE1cb8ab9bff500200158bffffffffff62^userDYNAMIC90d1921e5f510100a9ad2572f2b477fe';

	var grGroupMember = new GlideRecord('sys_user_grmember');
	grGroupMember.addEncodedQuery(queryStr); 
	grGroupMember.setLimit(1);
	grGroupMember._query();

	var answer = false;
	if(grGroupMember.hasNext()) {
		answer = true;
	}

	return answer;

})();

= tested!

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

14 REPLIES 14

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

the id variable is holding any value?

Are you saying if logger in user's group type is 1cb8ab9bff500200158bffffffffff62 then user criteria will return true?

Regards
Ankur

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

Hello Ankur,
The SYS ID 1cb8ab9bff500200158bffffffffff62  is the one for group type "ITIL" (it's an out of the box type and your instance should have the same sys ID)

And to answer your question yes, If the user is in a group of that type I need the user criteria to allow the access.

Thanks

Hi,

please try below code and check

answer = evaluateCriteria();

function evaluateCriteria() {
	var id = gs.getUserID(); //gets logged in user
	var gr = new GlideRecord('sys_user_grmember');
	gr.addQuery('user', id);
	gr.addQuery('group.type', '1cb8ab9bff500200158bffffffffff62');
        gr.setLimit(1);
	gr.query();
	return gr.hasNext();
}

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

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Is this the full code? For example, already on line 3, what is id? I don't see this anywhere? Also you are using gr.type to compare something, though there's no gr.next? Consider moving the gr.type into your query.

I would expect code like:

(function() {

	var id = 'something?';
	
	var gr = new GlideRecord('sys_user_group');
	gr.addActiveQuery();
	gr.addQuery('name', id);
	gr.addQuery('type', '1cb8ab9bff500200158bffffffffff62');
	gr.setLimit(1);
	gr.query();

	var answer = false;
	if(gr.hasNext()) {
		answer = true;
	}
	
	return answer;
	
})();

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn