Using the condition Builder

NeilH2
Giga Guru

I'm trying to wrap my head around using the condition builder, and would appreciate any help you could give.

 

Specifically i want to know how ServiceNow checks the condition specified on a record is a match to the current record

 

For example: How does ServiceNow check the Client template conditions match the current incident field values?

1 ACCEPTED SOLUTION

tltoulson
Kilo Sage

Hi Neil,



The first thing to understand about the Condition Builder is that it stores its result as an Encoded Query.   This is the same Encoded Query format used in the URL sysparm_query parameter, reference qualifiers, and List filters.   This can give you an indication of some ways on how to make use of it.   For instance, in the following examples I will assume a custom u_condition field is the condition builder and condGR is a GlideRecord containing the field:



Check for a Record Match Using GlideRecord




The advantage of this approach is it uses documented code only



var gr = new GlideRecord('incident'); // Lets check to see if an incident matches the condition


gr.addEncodedQuery(condGR.u_condition);


gr.addQuery('sys_id', 'Inicdent Sys ID here');


gr.setLimit(1); // We only need to know if the one Record Matches


gr.query();


if (gr.next()) {


      // Record matches the condition


}



Check for a Record Match Using GlideFilter



This method uses an undocumented Glide API that you can find in a few places in OOB scripts.   GlideFilter accepts two parameters.   The first is the GlideRecord object to check against a condition.   The second is the condition string itself.   This function returns true or false depending on whether the record matches or not.



var gr = new GlideRecord('incident');


gr.get('Incident Sys ID here');


if (GlideFilter.checkRecord(gr, condGr.u_condition)) {


        // Record matches the condition


}



I hope this has helped.   Please let me know if you need more information.


View solution in original post

7 REPLIES 7

Hi Travis - I just posted a blog article GlideFilter and Regular Expression Match   summarizing all I know about GlideFilter, and also referenced your post. Any feedback would be great. Thanks.



Please feel free to connect, follow, mark helpful / answer, like, endorse.


John Chun, PhD PMP see John's LinkedIn profile

visit snowaid


ServiceNow Advocate

This is super cool. I was struggling where addEncodedQuery was not working in all cases. GlideFilter.checkRecord() seems to be working in all cases. I am testing out all scenario. Thanks for the solution!!

Michael Domke
Tera Guru

This video from TechNow was a huge help for me in understanding the use of Conditions fields:


TechNow - Condition Fields



I'm finding more and more uses for Condition fields.



Michael