- 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
For your script its not required to add Encoded Query, because there are less conditions here. If there more condition logic then encoded query could be written
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.
********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect
Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
thanks for your reply.
No, I want to know how I can write this in encoded query.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Srini19
Which line number you are trying to make encoded?
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.
********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect
Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
this should work fine
var gm = new GlideRecord('sys_user_group');
gm.addQuery('active', true);
gm.query();
while (gm.next()) {
var gr = new GlideRecord('sys_user_grmember');
// Use encoded query: group = group_sys_id and user is empty
gr.addEncodedQuery('group=' + gm.sys_id + '^userISEMPTY');
gr.query();
if (!gr.next()) {
gs.print("This group has no member----" + gm.name);
}
}
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
