
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.
DIFFICULTY LEVEL: BEIGINNERish
Assumes good beginner to intermediate level knowledge and/or familiarity of Scripting in ServiceNow.
Did you know that there are several ways to do an OR query in a GlideRecord? These are interesting in that each actually is used throughout the platform. They are all interchangeable, and there are ones that are easier to maintain than others. I start with the hardest to maintain and least extensible and then move to the easiest and most extensible.
Let's start with the hardest to maintain, and most commonly used in the platform: The regular OR condition.
var incidentRecords = new GlideRecord('incident');
var incQuery = incidentRecords.addQuery('location.name', 'San Diego');
incQuery.addOrCondition('location.name', 'Salt Lake City');
incidentRecords.query();
gs.info('Regular OR Incident count: {0}', incidentRecords.getRowCount());
while (incidentRecords.next()) {
var number = incidentRecords.getValue('number');
var location = incidentRecords.location.name + '';
gs.info('---> number: {0} - location: {1}',
[number, location]);
}
Which gives us the following result:
I have over 150 Salt Lake City location records and a bit less than that in San Diego records. So, keep track of the number of records; that is the important part to make sure the OR is the same through all the examples.
The next example is OR Condition chaining. Here you do the same kind of thing as the previous query, but you can actually "DOT" them together (btw, it used to not like it if you put the dotted one on the next line - it errored right out, but they SN team seems to have corrected that "feature" and it appears to work fine now).
var incidentRecords = new GlideRecord('incident');
incidentRecords.addQuery('location.name', 'San Diego')
.addOrCondition('location.name', 'Salt Lake City');
incidentRecords.query();
gs.info('Regular OR Incident count: {0}', incidentRecords.getRowCount());
while (incidentRecords.next()) {
var number = incidentRecords.getValue('number');
var location = incidentRecords.location.name + '';
gs.info('---> number: {0} - location: {1}',
[number, location]);
}
Which gives us the following result:
Hmmm, I must be living right. That gave me the same result...but of course it did! It was planned!
Next is the ENCODED OR query. Here you use the encoded version of the OR to designate the same query (the sql variable). I view this is more maintainable than the previous two examples, but you have to do a good job formatting the query with splitting it to other lines concatenated together if it gets too large.
var sql = 'location.name=SAN DIEGO^ORlocation.name=SALT LAKE CITY';
var incidentRecords = new GlideRecord('incident');
incidentRecords.addEncodedQuery(sql);
incidentRecords.query();
gs.info('Regular OR Incident count: {0}', incidentRecords.getRowCount());
while (incidentRecords.next()) {
var number = incidentRecords.getValue('number');
var location = incidentRecords.location.name + '';
gs.info('---> number: {0} - location: {1}',
[number, location]);
}
Still tracking on the same result as the other two:
The final example is to use an IN statement with an array of values. This is the easiest to maintain as it is very visible/easy-to-read. It is also extensible (e.g. you can add more values to the array at a later time). The only draw-back is that adds a bit of complexity to the code solution (not much though).
var locationList = ['San Diego', 'Salt Lake City'];
var incidentRecords = new GlideRecord('incident');
incidentRecords.addQuery('location.name', 'IN', locationList.join(','));
incidentRecords.query();
gs.info('Regular OR Incident count: {0}', incidentRecords.getRowCount());
while (incidentRecords.next()) {
var number = incidentRecords.getValue('number');
var location = incidentRecords.location.name + '';
gs.info('---> number: {0} - location: {1}',
[number, location]);
}
Aaaand, the same result again! I use that last type of OR condition a lot, btw.
There you go, four different ways to do OR conditions in a GlideRecord query (also works with GlideAggregate).
Enjoy!
Steven Bell.
If you find this article helps you, don't forget to log in and mark it as "Helpful"!
Originally published on: 08-13-2015 04:26 PM
I updated the code and brought the article into alignment with my new formatting standard.
- 898 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.