SN101: Boolean logic and ServiceNow's Condition Builder

This article is part one of a multi-part series called "SN101", in which we'll explore some of the basic functionality of ServiceNow in beautiful detail. While the "SN101" articles are aimed toward newcomers to the ServiceNow platform, or new JavaScript developers, there is very likely to be something for everyone in each article. Thus, even if you are an experienced developer, it is recommended that you read the SN101 articles in chronological order.

Most of you will probably be aware of the fact that if you click the Filter icon at the top-left of a list-view, you'll be presented with a query builder UI. 

What many people aren't aware of, is the ability to control the "Order of ORperations"!

Get it? OR-perations? ...

Anyway, it's simple enough to string conditions together in a query like so: 

In the example above, we've got multiple "condition groups". Group one consists of 3 conditions, and group 2 is a single condition. In group 1, all of the conditions except the first begin with "OR". This tells us that they're logically grouped. 

You can think of each condition as evaluating to a boolean (TRUE/FALSE) value. Whenever you're dealing with boolean values that are grouped together, you have to evaluate them together. Think of it this way -- If you ask a question with multiple sub-questions, a single answer will suffice (though may be frustrating in conversation) for both. 

My name is Tim. I am 28 years old. Let's have a look at some examples of boolean logic, given that information and a few questions: 

Question: "Is your name Bob, OR are you more than 30 years old?" (FALSE or FALSE)
Answer: "No" (FALSE) -- Neither of the conditions evaluated to true, so even though they were joined by an "or", the answer is FALSE. 

Question: "Is your name Bob, OR are you less than 30 years old?" (FALSE or TRUE)
Answer: "Yes" (TRUE) -- Because one of the two expressions evaluated to true, and because they're joined by an "OR" (meaning if EITHER situation is true; not necessarily both), I would say Yes to this question. However in conversation, this would obviously leave it ambiguous as to which one is true, or if both of them are. 

Now that we understand boolean logic basically, let's have a look at what's called a "State table" for "AND" and "OR". A state table simply tells you: given a certain BOOLEAN input, what would be the output? 

ExpressionResult
AND
TRUE AND TRUE TRUE
TRUE AND FALSE FALSE
FALSE AND TRUE FALSE
FALSE AND FALSE FALSE
OR
TRUE OR TRUE TRUE
TRUE OR FALSE TRUE
FALSE OR TRUE TRUE
FALSE OR FALSE FALSE
Pro-tip: One of the best ways to understand deep boolean logic (beyond anything you're likely to have to use in ServiceNow) I recommend learning about NAND logic, which includes additional operators such as NAND, NOR, XOR, and XNOR. 

The basic logic in the above table extends to any number of booleans strung together. Let's apply this logic to the conditions in our condition builder from earlier: 

Let's imagine comparing the three conditions in group 1, to an imaginary ticket with number: "INC223214". Here's what those results look like: 

ExpressionResult
Number Contains 123 FALSE
Number Contains 321 TRUE
Number Contains 456 FALSE
Final result: TRUE

So based on that logic, group 1 would evaluate to TRUE. Group 2 however, is not an "OR" condition (and is therefore, an AND condition). If our imaginary ticket were indeed set to active, then the second group would also evaluate to true. As we saw in the logic table above, "TRUE AND TRUE" evaluates to true. However, if the ticket were not set to Active, the logical expression relating to the two groups would be "TRUE AND FALSE" which of course, further evaluates to simply FALSE. This means that our imaginary ticket would not show up in the results of this query if it were not set to active. In order for an item to show up in a filtered list, the overall logic of the filter, when applied to the entry, must evaluate to true. 

Now that we understand boolean logic in ServiceNow as well as in general, the "SN101" series will move on to more advanced topics as we continue our exploration of ServiceNow's queries and query builder. Stay tuned!