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

Its_Azar
Tera Guru
Tera Guru

Hi there @Srini19 

 

You can simplify this using an encoded query, but it’s a bit tricky since you’re referencing a related table. The issue is that dot-walking in encoded queries doesn’t work the same way across tables.

In your case, instead of joining via group.name, try this logic:

var gm = new GlideRecord('sys_user_group');
gm.addEncodedQuery('active=true^NOTEXISTSJOINsys_user_grmember.group=sys_user_group.sys_id');
gm.query();
while (gm.next()) {
   gs.print('This group has no members ---- ' + gm.name);
}

 

☑️ If this helped, please mark it as Helpful or Accept Solution so others can find the answer too.

Kind Regards,

Mohamed Azarudeen Z

Developer @ KPMG