Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

GlideRecord addorcondition with muliple conditions

anfield
Tera Guru

Can I add and andorcondition that includes more than one query ?

For example I want to do the following:

The only thing common between all 3 queries is the name = department, but I cant use it in the OR condition from the addQuery piece. I understand they are completely separate. Any advice? It would be easier not to use an encoded query as I have to slice up and join together strings etc. Thanks

var grd = new GlideRecord('cmn_department');
grd.addQuery('parent.name', location_first);
grd.addQuery('name', '=', department);

grd.addorcondition('parent.name', location_second)  AND ('name', '=', department);

grd.addorcondition(''parent.name', location_third) AND ('name' '=', department);

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@anfield 

since name=department is common query you should use addQuery()

for rest use addOrCondition()

updated below script; please try

var grd = new GlideRecord('cmn_department');

grd.addQuery('name', '=', department);

var qc = grd.addQuery('parent.name', location_first);

qc.addOrCondition('parent.name', location_second);

qc.addOrCondition('parent.name', location_third);

qc.query();

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Tried this and it doesnt work. Returns nothing, the logging doesnt even show in the logs

 

var grd = new GlideRecord('cmn_department');
grd.addQuery('name', department);
var gc1 = grd.addQuery('parent.name', location_first);
qc1.addOrCondition('parent.name', location_second);
qc1.addOrCondition('parent.name', location_third);

 

grd.query();

gs.log('lookup count: ' + grd.getRowCount());

Hi,

please try this; ensure you give valid values in the query

var grd = new GlideRecord('cmn_department');
var gc1 = grd.addQuery('parent.name', location_first);
qc1.addOrCondition('parent.name', location_second);
qc1.addOrCondition('parent.name', location_third);

grd.addQuery('name', department);

grd.query();

gs.log('lookup count: ' + grd.getRowCount());

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

The above didnt work either. The data values being used for the query are correct. I checked. I wanted to the first condition by itself before adding on the additional OR queries to validate. I validated again that this works (returned 1 result)

 

var grd = new GlideRecord('cmn_department');
grd.addQuery('parent.name', location_first);
grd.addQuery('name', '=', department);

grd.query();

 

This however returned 13 results, why would that be? It didnt evaludate the second part. It ran the second line on its own

 

var grd = new GlideRecord('cmn_department');

var q1 = grd.addQuery('name', department);
q1.addQuery('parent.name', location_first);

grd.query();