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

Bhavesh Jain1
Giga Guru

This is a logic which runs in the background and not exposed anywhere. In case you already have a requirement, you could mention so we can see if we have a solution for that case.








Regards,


Bhavesh


IGate-logo.png


http://www.igate.com


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.


Hi Travis



I think this is putting me on the right track.



Lets assume I am creating a new incident and i want to check a table for a matching condition if it finds one then I need an alert on screen.



Create Incident > Check Table > Find Matching Condition > Throw up an alert



I've managed to get as far as the below, but i'm not sure i'm passing the condition correctly, as i'm getting no results.


Adding messages into the code shows it stops after getting the table name



var alrt= new GlideRecord('u_alert');


alrt.addQuery('u_table', current.getTableName());


alrt.addEncodedQuery(condGR.u_conditions);  


alrt.query();


while (alrt.next()){


  gs.addInfoMessage('Alert' + arlt.alrt_name);


}


Hi Neil,



I am going to make a couple of assumptions here so please let me know if I am off track.   You have a u_alert table.   That table contains a u_table and a u_conditions field.   When an Incident is inserted, you need to find any u_alert messages that match the incident record.



If I have this correct, we actually need to invert the script a little.   We need to loop through ALL u_alert records and check them against the current record.



// Self executing anonymous function is not necessary but a good practice


(function() {


        // Find all u_alert records on the Incident table


        var alrt = new GlideRecord('u_alert');


        alrt.addQuery('u_table', current.getTableName());


        alrt.query();


        // Iterate records


        while (alrt.next()) {


                  // If the current Incident matches the Alert conditions


                  if (GlideFilter.checkRecord(current, alrt.u_conditions)) {


                            // Output the Message


                            gs.addInfoMessage('Alert' + arlt.u_name);


                  }


        }


})();