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.

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

Hi,

 

In that case you need to change the table to 'sys_user_grmember' in the script above & replace 'type' to 'group.type' which would be something like below.

answer = applyrestriction();

function applyrestriction() {
	var id = gs.getUser().getRecord().getValue('name'); //gets name of logged in user
	var gr = new GlideRecord('sys_user_grmember');
	gr.addActiveQuery();
	gr.addQuery('user.name', id);
	gr.addQuery('group.type', '1cb8ab9bff500200158bffffffffff62');
        gr.setLimit(1);
	gr.query();
	if(gr.hasNext()) {
		return true;
	}
	
	return false;
	
}

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

Thank you so much Mark,

Your script worked. 

I really appreciate your help, I also really appreciate the article you posted below, it's a great resource, it's going in to my bookmarks for sure.

Best

Lorenzo

Awesome! Just what I needed. Thanks how sharing, Mark.

Mark Roethof
Tera Patron
Tera Patron

Hi Lorenzo,

Here's an useful article I wrote a while ago:
Utilizing the breadcrumb on lists to generate your query

With using this, you could set up your queries a lot easier. I also used this for the solution I provided here.

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