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

Hi Brad, I have found your tips very useful. But I'm having an issue when the list renders in my database view on the front-end. Instead of returning the records I'm expecting from the script in my Before Query BR, I get all records from both my tables which doesn't make sense. All of the logging I've performed indicates that the query is actually working. I can log out the results perfectly with the results I would expect to see when the list renders in the browser.

When I log the following part of the script:
current.addQuery('sys_id', 'IN', answerArr.join(','))

I get "sys_idNotValidnull" back in the system logs.

Wondering if you've come across this message before?

I have not encountered that message before, but your results make sense in that when an addQuery or addEncodedQuery line cannot be interpreted correctly it is ignored, so that's why you are seeing all records.  What does your full BR script look like?

Here is my db view:
db_view_setup.png

 

And here is my BR:

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


Thank you for having a look!

I re-created your view and BR in my PDI, and I get the expected results, so I'm not sure what could be different in your environment that answerArr is becoming invalid.

Okay! I appreciate the extra set of eyes. I'll probably end up opening a case with support and see what they say. Thanks again!