Skip to main content

Reverse engineering ServiceNow encoded queries

 useful link:

https://www.servicenow.com/community/developer-blog/decoding-encoded-queries/ba-p/2290638



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

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

Comments

  1. The breakdown of ServiceNow encoded queries makes it much easier to understand how complex filtering conditions translate into GlideRecord scripts. The explanation of ^, ^OR, and ^NQ is particularly useful because these operators affect how conditions are grouped and how alternative criteria are evaluated. The RLQUERY example also highlights that encoded queries can express relationship-based conditions beyond simple field comparisons.

    For developers practising these concepts, Javascript Course in Chennai can help strengthen the JavaScript fundamentals needed to understand variables, method chaining, and conditional query construction. Translating encoded queries into readable code also makes debugging and maintaining ServiceNow scripts easier.

    ReplyDelete
  2. The alternative approaches using addQuery(), addActiveQuery(), and chained OR conditions illustrate how developers can express filtering logic programmatically. Exploring Javascript Training in Chennai can support practical scripting skills, while testing the resulting encoded query against known records helps verify that the intended filtering logic is preserved.

    ReplyDelete

Post a Comment