How To Use addOrCondition() with GlideRecord

Want To Add An OR Condition To A GlideRecord Query? As an admin, you need to be comfortable with GlideRecord scripting on the server. The addOrCondition() is amazingly helpful and I use it on a …

addOrCondition being used in a background script

Buy The "ServiceNow Developer's Manual" Now

We've packed over a decade of ServiceNow Development advice, into a single e-book.

Buy It Now

Want To Add An OR Condition To A GlideRecord Query?

As an admin, you need to be comfortable with GlideRecord scripting on the server.

The addOrCondition() is amazingly helpful and I use it on a weekly basis when writing out a business rule or script include.

Adding an OR conditional statement to a server side script, allows you to build more enhanced and stronger GlideRecord queries.

This can be run in any server side operation, so think business rule or script include – and not a client script.

Let’s look at how to build them out.

How To Use addOrCondition()

So here’s the situation. You want to look for high priority queries and you want to just print them out.

For example, you want to print out the last 5 P1 and P2 Incidents, to gather some data on them.

When you build out a GlideRecord Query, when using addQuery(), and you try to select both – the query will return empty.

So this will not work how you want or expect it to:

var incident = new GlideRecord(‘incident’);
incident.addQuery(‘priority’,’1′);
incident.addQuery(‘priority’,’2′);
incident.query();

while(incident.next()){
gs.print(incident.number);
}

This will return nothing.

But you’re trying to return BOTH priorities.

You want to return both Priority 1 OR Priority 2 Incidents.

So here’s where addOrCondition() comes into play. 

Your script would look like the following, utilizing this new method:

var incident = new GlideRecord(‘incident’);
incident.addQuery(‘priority’,’1′).addOrCondition(‘priority’,’2′);
incident.query();

while(incident.next()){
gs.print(incident.number);
}

See how we replace one of the addQuery() lines, and we instead append it to the previous addQuery(), but change it to an addOrCondition()?

Now all this example does is print out all of the Priority 1 and Priority 2 incidents in your environment.

For the example, we went with a super simple use case. But you can combine ANY two columns on any table, and have the GlideRecord return rows where any OR condition is required.

So you wouldn’t be using this exact script in your environment most likely. 

But it proves the point on how to return the data and work with it in ServiceNow.

Final Thoughts

Keep in mind that you also can’t use the addOrCondition() in a Client Script.

Well I guess technically you “can”, but it’s bad practice and not something we’re going to recommend doing here.

If you need to query a table from the client, look into GlideAjax and how that works.

There are a couple different varieties out in the wild on how you may see this built out, but this version makes the most sense to me.

It’s super straight forward and is cleaner than the other use cases of it.

Have you ever used addOrCondition()?

Is there a better way that you’ve seen it done?

Let us know below.



What Do You Think About This Article?

0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] Let’s start building out a simple query here, using our encoded query. Both of these queries simply print out the incident number, as an example. But we are proving that the query builds are returning the same exact record. For the longer example, we’ll use a couple addQuery lines with an addOrCondition. If you want to learn more about the addOrCondition method, check out our post here. […]

1
0
Would love your thoughts, please comment.x
()
x