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 Bowman
Kilo Patron
Kilo Patron

What is it you would be trying to show in the report - one row for every change_request record that appears in the list field on every custom table record, or are you trying to further filter on only certain change_request records by number or some other field?

Adam Stout
ServiceNow Employee
ServiceNow Employee

Database views are limited on the join operations.  You won't find INSTR and CONCAT because they aren't supported.  Here is what is:  https://docs.servicenow.com/csh?version=latest&topicname=t_AddATableToTheDatabaseView

List fields are changing to report on.  Here is a post that describes some options:  https://community.servicenow.com/community?id=community_blog&sys_id=36ee942edb1304501cd8a345ca961974 

 

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.

 

In the query business rule I used this for calling the Dynamic Filter for "Is one of my groups".

current.addEncodedQuery('list_field_nameDYNAMICd6435e965f510100a9ad2572f2b47744');