GlideRecord-Query with OR-Condition (Client-Script)

jens_sn
Kilo Contributor

Hello,

I try to do a query in a client-script with an or-condition. What I would like to achieve:
Query the Configuration Items of a Caller.


When I execute the script as background-script it works. But when I try to execute it as client-script I get the error that the variable (qc2) is an undefined function...

Here ´s my example code (which I excute as background script):

var newValue = 'a4425491880d2400ad10795120f63dd5' // Test-Value (Caller_id)

var gr = new GlideRecord('cmdb_ci');
var qc1 = gr.addQuery('owned_by',newValue); // Query by Caller
var qc2 = gr.addQuery('name', 'STARTSWITH', 'Notebook') // Condition1
qc2.addOrCondition('name', 'STARTSWITH', 'PC'); // Condition2
gr.query();
while (gr.next()){

if(gr.ip_address != ''){
gs.log(gr.name); // Test for Background-Script
alert(gr.name); // Test in Incident-Module
}
}

Does anybody know what ´s wrong?

5 REPLIES 5

benlittle
Tera Expert

Harkening back to high school math, DeMorgans law says you can always write ORs as ANDs and vice versa. The simple rule is

1) replace each condition with a single variable

2) Invert each element

3) Swap the operator

4) Invert the whole thing

5) Put back each condition

 

Take for example the following that you want to achieve:

country = France OR country = USA

 

let p replace 'country = France'

let q replace 'country = USA'

Step 1 - replace with a single variable --> p OR q

Step 2 - invert each element --> !p OR !q

Step 3 - Swap the operator --> !p AND !q

Step 4 - Invert the whole thing --> ! (!p AND !q)

Step 5 - Put back your conditions --> ! (Country != France AND Country != USA)

 

Simple translation in javascript:

! ( p || q ) == ( !p && !q )

 

Just make sure that when you 'invert' the operator, you know what the inversion is.

== inverted is !=

> inverted is <=

< inverted is >=

It obviously doesn't work with substring checks like STARTSWITH, but works when you can invert the operator.