- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2021 08:32 AM
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.
Solved! Go to Solution.
- Labels:
-
Reporting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2021 02:11 PM
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2023 01:42 PM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2023 04:42 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2023 09:05 AM
Here is my db view:
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2023 10:24 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2023 10:33 AM
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!