- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
One of the easiest ways to get an encoded query to use in a GlideRecord script is to go to the list view and build the query using the condition builder.
Then run it.
After running it right click on the last crumb of the breadcrumb and choose to "Copy the query" which gives you the encoded query.
*copied query looks like this: group.name=Software^user.nameISEMPTY
You can use that query as a string for the .addEncodedQuery() method. Of course replacing any portion that needs to be dynamic if applicable.
Example:
//non-dynamic example
var gm = new GlideRecord('sys_user_group');
gm.addQuery('active', true);
gm.query();
while (gm.next()) {
var gr = new GlideRecord('sys_user_grmember');
gr.addEncodedQuery('group.name=Software^user.nameISEMPTY');
gr.query();
if (gr.next()) {
gs.print("This group has no member----" + gr.group.name);
}
}
//dynamic example
var gm = new GlideRecord('sys_user_group');
gm.addQuery('active', true);
gm.query();
while (gm.next()) {
var eq = 'group.name=' + gm.name + '^user.nameISEMPTY';
var gr = new GlideRecord('sys_user_grmember');
gr.addEncodedQuery(eq);
gr.query();
if (gr.next()) {
gs.info("This group has no member----gm.name: {0}\n----gr.group.name: {1}", gm.name, gr.group.name);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Srini19 ,
var gr = new GlideRecord('sys_user_group');
gr.addEncodedQuery('active=true^sys_idNOT INsys_user_grmember.group');
gr.query();
while (gr.next()) {
gs.print("This group has no member----" + gr.name);
}active=true ensures you are only checking active groups.
sys_idNOT INsys_user_grmember.group means you are selecting groups whose sys_id does not exist in the group field of any sys_user_grmember record, i.e., groups with no members.
Please appreciate my efforts, help and support extended to you by clicking on – “Accept as Solution”; button under my answer. It will motivate me to help others as well.
Thanks & Regards,
Mohammed Mustaq Shaik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
are you sure this script will work?
It would be nice if you could share a working example with output so that it helps other members.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Ankur Bawiskar ,
I have executed in my PDI i got the output like
Correct me if i am wrong Ankur i will correct it, Thanks
Warm Regards,
Shaik Mustaq.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Did you verify the output i.e. the groups printed really don't have any members in it?
The script didn't work for me, it gave incorrect output.
See below it gave me this group has no member "Colorado Dispatchers" but it has members in it
Please make sure you share the correct script or else members get confused and it increases the time to close the thread.
Thanks !
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
One of the easiest ways to get an encoded query to use in a GlideRecord script is to go to the list view and build the query using the condition builder.
Then run it.
After running it right click on the last crumb of the breadcrumb and choose to "Copy the query" which gives you the encoded query.
*copied query looks like this: group.name=Software^user.nameISEMPTY
You can use that query as a string for the .addEncodedQuery() method. Of course replacing any portion that needs to be dynamic if applicable.
Example:
//non-dynamic example
var gm = new GlideRecord('sys_user_group');
gm.addQuery('active', true);
gm.query();
while (gm.next()) {
var gr = new GlideRecord('sys_user_grmember');
gr.addEncodedQuery('group.name=Software^user.nameISEMPTY');
gr.query();
if (gr.next()) {
gs.print("This group has no member----" + gr.group.name);
}
}
//dynamic example
var gm = new GlideRecord('sys_user_group');
gm.addQuery('active', true);
gm.query();
while (gm.next()) {
var eq = 'group.name=' + gm.name + '^user.nameISEMPTY';
var gr = new GlideRecord('sys_user_grmember');
gr.addEncodedQuery(eq);
gr.query();
if (gr.next()) {
gs.info("This group has no member----gm.name: {0}\n----gr.group.name: {1}", gm.name, gr.group.name);
}
}
