Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

addOrCondition() not working with addQuery() function !!

shivcharan
Kilo Explorer

Hello,

I need to write a multiple AND query along with OR conditions for addQuery() function.

When I try to write the following piece of code :-

var gr = new GlideRecord('sys_update_xml');

gr.addQuery('sys_updated_by',a).addOrCondition('sys_updated_by',c).addOrCondition('sys_updated_by',d);

gr.addQuery('target_name',aa).addOrCondition('target_name',ab);

gr.query();


I am writing it as a CLIENT SCRIPT. I also tried it by running it in the WORKFLOW but nothing worked.

Kindly help.

5 REPLIES 5

The SN Nerd
Giga Sage
Giga Sage

You should not be using GlideRecord to query the database on the client side.


You need to do this in a business rule and call it via GlideAjax.



Also, OR conditions are done like this



var gr =new GlideRecord('incident');


var qc = gr.addQuery('category', 'hardware');


qc.addOrCondition('category', 'software');


gr.addQuery('priority','1');



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Michael Kaufman
Giga Guru

Paul is right, you might have to do this via a Business Rule or Glide Ajax.   Client scripts are limited with Glide Record queries. There are some things you can't do, due to performance reasons.



Client Side GlideRecord - ServiceNow Wiki


Ashish Kumar Ag
Kilo Guru

Hi,



Try Below, make sure addQuery should have object of gliderecord.. here gr



var gr = new GlideRecord('sys_update_xml');


var pr = gr.addQuery('sys_updated_by',a);


  pr.addOrCondition('sys_updated_by',c);


  pr.addOrCondition('sys_updated_by',d);


var qr = gr.addQuery('target_name',aa);


  qr.addOrCondition('target_name',ab);


gr.query();


Then wrap it up into a GlideAjax Script Include and call it with your client script!



Script include:


var AjaxExample = Class.create();


AjaxExample.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  getUpdateSets: function(){


  var gr = new GlideRecord('sys_update_xml');


  var pr = gr.addQuery('sys_updated_by',a);


  pr.addOrCondition('sys_updated_by',c);


  pr.addOrCondition('sys_updated_by',d);


  var qr = gr.addQuery('target_name',aa);


  qr.addOrCondition('target_name',ab);


  gr.query();


  while (gr.next() ) {


  //Do processing here


  }


  return gr.getRowCount();


  }


}



Client script


var ga = new GlideAjax('AjaxExample');


ga.addParam('sysparm_name','getUpdateSets');


ga.getXML(HelloWorldParse);



function serverResponse(response) {


    var answer = response.responseXML.documentElement.getAttribute("answer");


    alert(answer);


}



Remember, you can always do it in a business rule if there is no requirement to do it client side!



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022