Issue with Nested GlideRecord's and Dot walk fields

Kishore32
Tera Contributor

Hello,

I've a requirement to check whether the user is part of a group. If it is then I've log it to file or something to do.

Here is my script and I'm not getting desired output, please let me know what is the issue with the script, is it because of dot walk fields. Please help me here.

var gr = new GlideRecord('sys_user');
gr.initialize();
gr.query();

while(gr.next()){
//gs.info(gr.user_name);
var gm = new GlideRecord('sys_user_grmember');
gm.initialize();
gm.addQuery('sys_user_grmember.user.sys_id',gr.sys_id);
gm.Query;
while(gm.next()){
gs.info('In');
if(gm.group.sys_id == 'aead02002f26011048be265df699b6a3'){
gs.info(gr.user_name);

//Do something
}
}
}

Appreciate any suggestions!!

Thanks 

 

1 ACCEPTED SOLUTION

Hi,

then do this

var gr = new GlideRecord('sys_user');
gr.query();
while(gr.next()){
	var gm = new GlideRecord('sys_user_grmember');
	gm.addQuery('group.name', 'Capacity');
	gm.addQuery('user', gr.getUniqueValue());
	gm.query();
	if(gm.next()){
		gs.info("User " + gr.getDisplayValue() + ' is member of ' + 'Capacity');
	}
}

Regards
Ankur

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

View solution in original post

7 REPLIES 7

eumak
Tera Guru

Hello Kishore,

If you just want to check some users from the server-side you can perform it via writing a BR

var grp = gs.getProperty('name of your group property');	//to get the group id via property
var userID = gs.getProperty('name of your user property'); // to get the user id via property	
var gr = new GlideRecord('sys_user_grmember'); 
gr.addQuery('user', userID); 
gr.addQuery('group', grp);
gr.query();
if(gr.next()){
  gs.info("User is Member of Group!");
 //Do something
}
else{
  gs.info("User is Not a Member of Group!");
//Dont do something
}

 

If you are want to perform some action on the basis of the user's presence in the group, Use the below code in BR

gs.getUser().isMemberOf(gs.getProperty('name of your property'))

Above will Returns true if the user is a member of the group you specified in your property. Returns false if not a member of that group. So with the above, it will execute (true) for users part of that group.

 

Cheers..!
Happy Learning
Tushar

Mark it as helpful or correct, If Applicable


Cheers..!

Happy Learning:)

Tushar

Kishore32
Tera Contributor

Thanks for the response.

But I'm looking for the script with in the Flow as I'm adding a custom action with script step.

I'll check the suggestions by all of you and will try.

 

Thanks

Abhijit4
Mega Sage

Hi Kishore,

If you want users who are member of group whose sys_id is 'aead02002f26011048be265df699b6a3' then you don't need to glide into user table, you can achieve the same by gliding into group membership table as shown below,

var grGroupMember = new GlideRecord('sys_user_grmember');
grGroupMember.addQuery('group', 'aead02002f26011048be265df699b6a3');
grGroupMember.query();
while(grGroupMember.next()){
gs.print("User"+grGroupMember.user.name+" is part of group"+grGroupMember.group.name);
}

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Thanks and Regards,
Abhijit
Community Rising Star 2022

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP