How can I create an Interactive Filter for Dashboard that has two options, Company1 or Not CompanyA

JR42
Giga Guru

I would like to add an Interactive Filter to a Dashboard that allows the user to filter the reports based on if the record's Company field is CompanyA, or, not CompanyA.

 

If 'Is CompanyA' is selected, it should filter the reports to only show records for CompanyA.

If 'Is Not CompanyA' is selected, it should filter the reports to show records from all companies, except CompanyA.

If neither is selected (filter is not used), the reports show records for all companies, including CompanyA.

 

CompanyA is my company.  All the other companies are customers.  This filter would allow me to use the same reports for reporting on all tickets, internal tickets, and/or customer tickets.

If I could figure this out it would be a huge help and reduce the number of dashboard tabs and reports I'll need to create.

1 ACCEPTED SOLUTION

I created this, which works per your requirements

Mike_R_0-1666125020235.png

 

Put this code in a dynamic content block (instructions are in the article that I sent before). This is a good starting point. just replace the sys_id with your company sys_id. You can also add some css styles/formatting to the buttons if necessary. 

 

<?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 compA() {
var filter_message = {};
filter_message.id = "my_unique_id";
filter_message.table = "task";

<!-- Add your own filter query logic here -->
filter_message.filter = "company=31bea3d53790200044e0bfc8bcbe5dec"; //SysID of company to show
SNC.canvas.interactiveFilters.setDefaultValue({
id: filter_message.id,
filters: [filter_message]
}, false);
my_dashboardMessageHandler.publishFilter(filter_message.table, filter_message.filter);
}

function notComA() {
var filter_message = {};
filter_message.id = "my_unique_id";
filter_message.table = "task";

<!-- Add your own filter query logic here -->
filter_message.filter = "company!=31bea3d53790200044e0bfc8bcbe5dec^ORcompany=NULL"; //SysID of company to exclude
SNC.canvas.interactiveFilters.setDefaultValue({
id: filter_message.id,
filters: [filter_message]
}, false);
my_dashboardMessageHandler.publishFilter(filter_message.table, filter_message.filter);
}

function clearFilter() {
var filter_message = {};
filter_message.id = "my_unique_id";
filter_message.table = "task";
filter_message.filter = "";
SNC.canvas.interactiveFilters.setDefaultValue({
id: filter_message.id,
filters: [filter_message]
}, false);
my_dashboardMessageHandler.removeFilter();
}

</script>
<input id="allTasks" type="button" value="All tasks" onclick="clearFilter();" />
<input id="CompA" type="button" value="Company A" onclick="compA();" />
<input id="notCompA" type="button" value="Not Company A" onclick="notComA();" />

</j:jelly>

 

View solution in original post

17 REPLIES 17

Mike_R
Kilo Patron
Kilo Patron

You'll need to create a custom interactive filter for this

 

https://docs.servicenow.com/bundle/sandiego-now-intelligence/page/use/dashboards/reference/r_CustomP...

 

this is a similar example taken from the link above

 

Define filtering logic - example

After you create the interactive filter, add the filtering logic.

Before you begin

Role required: admin

Filters use the DashboardMessageHandler class to manage active filters. Instantiate DashboardMessageHandler with a unique value.
Note: The ID of the custom interactive filter must be unique. If it has the same ID as another interactive filter or custom interactive filter, the filtering logic does work properly.

Procedure

  1. In the dynamic content record, add the filtering logic to the Dynamic content XML block.
  2. The Only mine button publishes a filter on Task table reports using the encoded query caller_idDYNAMIC90d1921e5f510100a9ad2572f2b477fe. The All tasks button removes the filter.
  3. You can then add buttons or other interface elements to the dynamic content. In this example, the code for the clearFilter() function and the buttons in the filter are added below the publishFilter() function.
    <?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 publishFilter () {
            var filter_message = {};        filter_message.id = "my_unique_id";        filter_message.table = "task";
        
        <!--     Add your own filter query logic here -->        filter_message.filter = "assigned_toDYNAMIC90d1921e5f510100a9ad2572f2b477fe";        SNC.canvas.interactiveFilters.setDefaultValue({                id: filter_message.id,                filters: [filter_message]
                }, false);        my_dashboardMessageHandler.publishFilter(filter_message.table, filter_message.filter);
        }
        
        function clearFilter() {
            var filter_message = {};        filter_message.id = "my_unique_id";        filter_message.table = "task";        filter_message.filter = "";        SNC.canvas.interactiveFilters.setDefaultValue({                id: filter_message.id,                filters: [filter_message]
                }, false);        my_dashboardMessageHandler.removeFilter();
        }
     </script>   
     
    Example of a filter that generates a static filter on 'task' table reports, or removes it <br/>
        <input id="allTasks" type="button" value="All tasks" onclick="clearFilter();" />
        <input id="onlyMine" type="button" value="Only mine" onclick="publishFilter();" />
        
    </j:jelly>

JR42
Giga Guru

Can anyone help me with the scripting?  I am very new to JavaScript.

Which reports/table are you trying to filter based on company. Incident table?

Hi Mike, there is a mixture of reports on the Dashboard, using the Incident and Task [task] tables.

 

I also realized that instead of directly using the Company name, I need to use 'contains' logic.  There are a number of companies that contain 'CompanyA'. 

 

With that in mind, my requirements would look more like:

- If 'Is CompanyA' is selected, it should filter the reports to only show records for companies that contain 'CompanyA' in the name.

- If 'Is Not CompanyA' is selected, it should filter the reports to show records from all companies that do not contain 'CompanyA' in the name.

- Then based on the example, I would need a third button that would filter the reports to show records for all companies.

 

Thanks for you help!