how to query the table with conditions using addQuery?

msm4
Mega Guru

Hi Team,

I am trying to get the data from the table, based on two conditions, the below snippet I am trying to pull the records from the table which has "type=x & type=y".

Can anyone please guide me.

<g:evaluate var="jvar_gr" object="true">

  var gr = new GlideRecord("table_name");

  var gr2=gr.addQuery('type','x');

    gr2.addorCondition('type','y');

  gr2.addEncodedQuery('beginONToday@javascript:gs.daysAgoStart(0)@javascript:gs.daysAgoEnd(0)')

  gr2.query();

  gr2;

</g:evaluate>

Thanks in advance.

1 ACCEPTED SOLUTION

It is addOrCondition, not addorCondition.



You can also string it: gr.addQuery('type','x').addOrCondition('type','y'); and forget the second variable declaration.


View solution in original post

8 REPLIES 8

Ajai S Nair
Giga Guru

Hi Smitha,



Please adjust the script like the one shown below:



  var gr = new GlideRecord("table_name");


var queryString = " beginONToday@javascript:gs.daysAgoStart(0)@javascript:gs.daysAgoEnd(0)";//Give the query string which you got from the list


  var gr2=gr.addQuery('type','x');


    gr2.addorCondition('type','y');


gr.addEncodedQuery(queryString);


gr.query();


while (gr.next()) {



//the operation you want to do for the records satisfying above condition


}



GlideRecord - ServiceNow Wiki



Hope this will help you


Hi Ajai,


I don't have any issues with addEncodedQuery , I am facing problem in pulling records of "type". Is   my code correct in quering ?


Hi smitha,



I have rearranged it a bit. Not only the encoded query. It is gr instead of gr2. Please check.


var gr2=gr.addQuery('type','x');


    gr2.addorCondition('type','y');



Here , I am getting the data only for the first query , it is not executing the second query.