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.

quesion on script

Srini19
Tera Contributor
    var gm = new GlideRecord('sys_user_group');
    gm.addQuery('active', true);
    gm.query();
    while (gm.next()) {
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('group.name', gm.name);
        gr.addQuery('user.name', '=', "");
        gr.query();
        if (gr.next()) {
            gs.print("This group has no member----" + gm.name);
        }
    }
 
 
 
This query is  working fine, how do I wirte this in addEncoded query when I tried I was getting error. Please advise.
1 ACCEPTED SOLUTION

ChrisBurks
Giga Sage

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.

ChrisBurks_0-1762918474498.png

*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);
        }
    }

 

View solution in original post

10 REPLIES 10

Me Being Mustaq
Tera Guru

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

@Me Being Mustaq 

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.

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

Hi @Ankur Bawiskar ,

 

I have executed in my PDI i got the output like

MeBeingMustaq_0-1762878651546.png

Correct me if i am wrong Ankur i will correct it, Thanks

 

Warm Regards,

Shaik Mustaq.

 

@Me Being Mustaq 

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

AnkurBawiskar_0-1762916519320.png

 

AnkurBawiskar_1-1762916535648.png

Please make sure you share the correct script or else members get confused and it increases the time to close the thread.

Thanks !

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

ChrisBurks
Giga Sage

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.

ChrisBurks_0-1762918474498.png

*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);
        }
    }