Database view where clause behavior

chuckn
Kilo Guru

I suspect this has been asked on the Community before, but trying to find a good explanation is a challenge.

 

I have the following in a Database view:

 

chuckn_0-1665083502916.png

 

The view loads, but it doesn't limit alm_hardware records to model_category of Computer (hdw_model_category='81feb9c137101000deeabfc8bcbe5dc4'). However, if I add a before query business rule and add that condition, it (unsurprisingly) works fine:

 

 

(function executeRule(current, previous /*null when async*/) {
	current.addEncodedQuery('hdw_model_category=81feb9c137101000deeabfc8bcbe5dc4');
})(current, previous);

 

 

Why doesn't that part of the where clause in the database view have any effect?

 

Thanks!

1 ACCEPTED SOLUTION

Soeren Maucher
Mega Sage

Hello @chuckn

 

I recreated your database view and had the same issue.  
According to a ServiceNow article the "'Where clause in a database view is primarily used to specify join conditions. It cannot be used to filter based on field values (ex. 'Active' = 'true')." ((Filtering using the 'Where clause' in database views))
However, ServiceNow is using this typeof "filtering" in some of their own out of the box database views. 
From what I understand and found out with try and error is that it seems to work for right joins, but not for left joins. I guess the left join basically overwrites the "=" condition. The idea of the left join is to join all entries from the first table (on the left), so after filtering them out they will be added again by the left join. 

In the following setup of the database view the "=" filter condition is working: 

6.PNG
Not sure if this will still fulfill your requirement.

I hope this helped! 

Greetings,
Sören

 

 

View solution in original post

3 REPLIES 3

Soeren Maucher
Mega Sage

Hello @chuckn

 

I recreated your database view and had the same issue.  
According to a ServiceNow article the "'Where clause in a database view is primarily used to specify join conditions. It cannot be used to filter based on field values (ex. 'Active' = 'true')." ((Filtering using the 'Where clause' in database views))
However, ServiceNow is using this typeof "filtering" in some of their own out of the box database views. 
From what I understand and found out with try and error is that it seems to work for right joins, but not for left joins. I guess the left join basically overwrites the "=" condition. The idea of the left join is to join all entries from the first table (on the left), so after filtering them out they will be added again by the left join. 

In the following setup of the database view the "=" filter condition is working: 

6.PNG
Not sure if this will still fulfill your requirement.

I hope this helped! 

Greetings,
Sören

 

 

chuckn
Kilo Guru

Thank you, Sören! I actually think I came across that KB article earlier in my search on a related topic, but the comment about not using Where to filter results didn't register. Setting all the view tables' Left join to false results in not returning alm_hardware records that do not have an assigned_to, which isn't what we're going for. The business rule is doing the trick, though, so we're good. Appreciate it! 

Adam Stout
ServiceNow Employee
ServiceNow Employee

When I have had similar use cases, I seem to remember that the where on the first table was ignored but if I moved it to a where clause on the second table, it worked.  So try adding a && and your first where to your second clause.