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

Mark Stanger
Giga Sage

'or' conditions in GlideRecord queries aren't available from the client. You can use an encoded query string to accomplish the same thing though. See the wiki for details on this workaround.

http://wiki.service-now.com/index.php?title=Client_Scripts#Using_GlideRecord_Query_from_client


Hello,

thanks for your reply and your hint!

That worked for me


I was just struggling with this same issue, so thanks for noting the limitation!



One other thing I always forget when writing my Glide queries: you can often use the 'IN' operator to replace simple "or" conditions (Using GlideRecord to Query Tables - ServiceNow Wiki). Of course, the 'IN' operator wouldn't work in this particular case (when looking for 'STARTSWITH'), but it's good to remember all of the operators that are available.


tim2222
Tera Expert

This is an old thread but the first that turns up in relation to performing "OR" conditions via Client-side GlideRecord.

Warning, what I'm suggesting here may break in a future version ...

A quick and dirty workaround is to prefix "OR" to the next condition (this example can be achieved with an IN, however my real-world needed equality or STARTSWITH):

var gr = new GlideRecord('sys_user');
gr.addQuery('user_name', 'guest');
gr.addQuery('ORuser_name', 'admin');
gr.query(function(gr) {
  console.log(gr.rows.length);
});