Custom Interactive Filters basics


A custom interactive filter can be used to filter multiple Reports using different tables. There are two different APIs to publish an interactive filter for a single table or multiple tables.

Please keep in mind the following points creating a custom interactive filter:


ServiceNow Docs site has an example regarding using the interactive filter for a single table. Since the API DashboardMessageHandler.publishMessage() is not documented, sometimes we see implementations using DashboardMessageHandler.publishFilter() multiple times.

  1. Create two DashboardMessageHandler instance and call the publishFilter() method two times. All the report widget following interactive filter refreshes 2 times.
  2. Some times when it is necessary to use the filter for more than 2 tables, an array is created and in a loop, N DashboardMessageHandler instances are constructed.  Since the publishFilter() method will be called N times, that will refresh all the reports following interactive filter N times which will most probably cause performance issues.

How to apply Custom Interactive Filter to multiple tables

The example below filters the Report using incident and problem tables by assignment group using a custom interactive filter. Ex:

DashboardMessageHandler.publishMessage() accepts multiple filters as an array, one can just construct the filter array properly then create only one instance of DashboardMessageHandler and then call DashboardMessageHandler.publishMessage([filters]). This will refresh the widget only one time and is more performant compared to calling DashboardMessageHandler.publishFilter() multiple times.


Example:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<script>
var my_dashboardMessageHandler = new DashboardMessageHandler("my_unique_id");

function publishMultipleFilter() {
<!-- Mutliple filters can be passed as an array -->
var finalFilter = [{"table":"incident","filter":"assignment_group=6fcd3b573b331300ad3cc9bb34efc447"}, {"table":"problem","filter":"assignment_group=6fcd3b573b331300ad3cc9bb34efc447"}];
<!-- call setDefaultValue first -->
SNC.canvas.interactiveFilters.setDefaultValue({
id: my_dashboardMessageHandler._unique_id,
filters: finalFilter,
}, false);

<!-- then call Publishmessage to publish filters-->
my_dashboardMessageHandler.publishMessage(finalFilter);
}

function clearFilter() {
SNC.canvas.interactiveFilters.removeDefaultValue(my_dashboardMessageHandler._unique_id, false);
my_dashboardMessageHandler.removeFilter();
}
</script>

Example of a filter that filters incident and problem table where assign_ment group is Problem solving <br/>
<input id="allTasks" type="button" value="All" onclick="clearFilter();" />
<input id="onlyMine" type="button" value="Assignment group is [Problem solving]" onclick="publishMultipleFilter();" />

</j:jelly>