The ServiceNow Nerd

The musings of a ServiceNow platform developer and enthusiast

The SN Nerd

Decoding Encoded Queries

by snnerd
Published: Last Updated on 2,083 views

Like Regular Expression, Encoded Queries are incredibly useful to write but incredibly hard to read.
To help with this, I created a diagram to help Decode Encoded Queries.

Converting Filters to Encoded Queries

Please see the color-coded diagram below. Please note that it uses Condition Builder v3, as it is easier to read and allows for Related List conditions.

Encoded Query

active=true^short_descriptionLIKEIssue^ORshort_descriptionLIKEProblem^NQactive=false^close_code=Not Solved (Not Reproducible)^RLQUERYtask_ci.task,>=1,m2m^ci_item.assignment_groupNSAMEASchange_control^ENDRLQUERY

Converting to Code

^ = AND

// active=true
var gr = new GlideRecord('incident');
gr.addQuery('active','true');

// Alternative 1
gr.addCondition('active','true');

// Alternative 2
gr.addActiveQuery();

^OR = OR

//active=true^short_descriptionLIKEIssue^ORshort_descriptionLIKEProblem
var qc = gr.addQuery('active',true);
qc.addOrCondtion('short_description','CONTAINS','Issue');
qc.addOrCondtion('short_description','CONTAINS','Problem');

//Alternative
gr.addQuery('active',true).addOrCondtion('short_description','CONTAINS','Issue').addOrCondtion('short_description','CONTAINS','Problem');

^NQ = NEW QUERY / CRITERIA

// ^NQactive=false^close_code=Not Solved (Not Reproducible)
var nq = new GlideRecord('incident');
nq.addQuery('active',false);
nq.addQuery('close_code','Not Solved (Not Reproducible)');

var queries = [];
queries.push(gr.getEncodedQuery());
queries.push(nq.getEncodedQuery());

var finalQry = queries.join("^NQ");

var gr = new GlideRecord('incident');
gr.addEncodedQuery(finalQry);

On Related List Conditions

These can be used in code, and Reference Qualifer Script Includes, but do not worked when pasted directly as an encoded query into a reference qualifier field.

See Encoded query strings for more documentation on the operators.

Related Posts

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More