Database View - List Field

PDOOK
Giga Contributor

We have a custom table which has a list field pointing to Change. A custom table record could have multiple CHGs listed in the list field.

For reporting purposes we created a Database View with a where clause of customtable_field = chg_sys_id, however the list field could have multiple CHGs listed and those particular records aren't appearing in our database view. I've scoured the community and attempted the INSTR and CONCAT functions but to no avail. Can anybody help? I've also seen the KB article which indicates that it isn't possible but thought to try here before admitting defeat. 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Never admit defeat!  Here's a way that you can setup a Database view between a custom table and the change_request table, only showing the rows of the custom table that have one or more change requests populated in the list field, and showing one row for each change in the custom table list.  The custom table column data is repeated for each row since the emphasis is showing a row for each change record represented in the list field.  In my example my custom table has 5 records that are populated with 1, 2, 3, 4, and 5 change request records in a field named u_change.  The end result is a Database View with 15 rows (5+4+3+2+1).

To do this, setup your view like so.

find_real_file.png

This should give you one row for each custom table record that has a value in the list field, plus one row for each change_request record.  Next, create a before Query Business Rule, using the Database View Name as the Table.  My Script on the Advanced tab looks like this.

(function executeRule(current, previous /*null when async*/ ) {
	var answerArr = [];
	var gr = new GlideRecord('u_list');
    gr.query();
    while(gr.next()){
		if(gr.bkb_u_change.indexOf(gr.chg_sys_id)>-1){
			answerArr.push(gr.sys_id.toString());
		}
    }
	current.addQuery('sys_id', 'IN', answerArr.join(','));
})(current, previous);

This is where we're doing the IN/contains join that can't be done in the view itself, then filtering the list results to only those records where the change sys_id is one of the custom table list field records.

 

View solution in original post

11 REPLIES 11

Brad, just following up on this as I figured out the issue with assistance from HI. The database view I created worked perfectly in my PDI but not in my company instance. It turns out the following system property breaks the database view business rule if it's set to true:
glide.invalid_query.returns_no_rows

 

It's set to false by default, which is why I didn't have the issue in my PDI. We require the property to be true to protect against bad queries, so it looks like I am stuck.

 

Thanks again for the help!

PDOOK
Giga Contributor

Wow Brad, that worked a treat. Thanks for that. Have saw quite a few queries like this on the community so hopefully this helps others too. Thanks again.