GlideRecord addorcondition with muliple conditions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2020 07:14 AM
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);
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2020 07:25 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2020 07:56 AM
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());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2020 08:08 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2020 09:24 AM
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();