MrMuhammad
Giga Sage

find_real_file.png

Interactive filters?

Interactive Filters allow you to filter report widgets directly from a homepage or dashboard without modifying the reports. Servicenow comes up with many filter types but sometimes it requires applying a filter based on the catalog item/requested item variables and available interactive filters don't help.

Fortunately, as an administrator, you can create scripted filters and add them to the dashboard and homepages. With that being said, we are going to create our own scripted filter based on catalog item variables.

Pre-requisite

knowledge of Javascript/JellyScript - 6+ months

Knowledge of HTML - 6+ months

knowledge of Reports and filters - Basic

Catalog item with a variable which will be used for the scripted interactive filter. 

What are we developing today?

I have a catalog item that contains a variable name Status of type select box. Three statuses are available as choices pending analysis, analysis in progress, and work in progress. I will be using that variable as a filter for our dashboard and upon selection of each status, a requested item list report will be filtered by that status.

Before diving into the development part, let's have a look at our end result 

find_real_file.png

Solution

First, we will create a Dynamic Content page. Go to target dashboard, click on plus/add widget icon from the grey header, and select content blocks from the widget category dropdown. Select New Dynamic Content block from the list below and click Add button. A new widget will be added on the left content pane. you can click on click here hyperlink available in the body of the widget. 

find_real_file.png

A new Dynamic Content Block form will open up. Our real scripting logic will go here. Please use the script below to create an HTML widget that contains the drop-down with all the choices for the status variable of the catalog item.

Please read the comments within the script carefully to understand the code and the required changes to make it work for you in your envrionment.

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:evaluate var="jvar_status" object="true" jelly="true">
	
var statusArray=[];
<!-- Get choices of select box [status] and pass it to the widget UI -->
var gr= new GlideRecord('question_choice');
  gr.addEncodedQuery('question=2eec46cc2f004d1023b0a55df699b65f'); <!-- replace the sys_id with the sys_id of your variable. -->
  gr.addOrderBy('name');
  gr.query();

  while(gr.next()){
    statusArray.push([gr.getValue('value'),gr.getValue('text')]);
  }
	
 statusArray;
</g:evaluate>
 
    <select id='filter_statuses' class='select2-search' onchange='filterStatus()'>
        <option value="">All</option>
        <j:forEach var="jvar_state" items="${jvar_status}"> 
            <option value="${jvar_state[0]}">${jvar_state[1]}</option>        
        </j:forEach>
    </select>   
 
    <script>
     var dbh = new DashboardMessageHandler("filter_status");
		
     function filterStatus(){
        var status = $j('#filter_statuses').val();
        if (status)
            dbh.publishFilter('sc_req_item', 'cat_item=c45927732f3b3c1023b0a55df699b636^variables.2eec46cc2f004d1023b0a55df699b65f=' + status); <!-- change the cat_item sys_id and variable.sys_id with actual sys id of catalog item and target variable. -->
        else
            dbh.removeFilter();
     }
     filterStatus();
</script>
 
</j:jelly>

 

After making the suggested changes in the script comments, save the form.

Open the dashboard and if the interactive filter doesn't appear then click on the add widget icon and select widget category as Content block and search for your content block created in the above step by name.

find_real_file.pngHover over the report widget title bar and select the settings cog to open the widget configuration. Select checkbox "Follow interactive filters" & "Show when following interactive filters".

find_real_file.pngCongrats, You are done!  

Changing the filter will now filter down the list of records based on the selection. 

Reference

https://docs.servicenow.com/bundle/rome-now-intelligence/page/use/dashboards/concept/c_CustomPublish...

Please mark it as helpful, bookmark it for future reference and provide feedback or ask questions in the comment box below.

 

Comments
MrMuhammad
Giga Sage

hi

MrMuhammad
Giga Sage

test

Maury S_nchez
Tera Explorer

Very good nformation and aplication, only I have a question, why when I enter the dashboard where the filter is, I choose an option, but the data in the graph does not change?

MrMuhammad
Giga Sage

Hi Maury, the Last step in the process is the answer to your question. Have you tried to set the Follow Interactive filter in the report widget configurations? Please refer the last screenshot above.

Bindhu1
Tera Contributor

Hi Muhammad, I have tried the All steps mentioned above but at last step when I click on Edit widget, I am unable to get Interactivity option. Please let me know the solution for that. 

Bindhu1_0-1669197291684.png

 

 
 
 


MrMuhammad
Giga Sage

Hey @Bindhu1 - That option will appear when you edit the target report widget. 

Amit Naik1
Tera Contributor

Can it be possible to select specific custom interactive filter should work of specific report in one dashboard and not for all ?

MrMuhammad
Giga Sage

Hi @Amit Naik1 ,

 

To the best of my knowledge, I believe that is not possible. All the reports following interactive filters will be affected by all the available interactive filters in the dashboard.

 

Regards,

Muhammad

Amit Naik1
Tera Contributor

script tag code is not working in Tokyo environment.

I have updated the code kindly review 

<script>
var dashboardMessageHandler = new DashboardMessageHandler("filter_status");
function filterStatus(){
var status = $j('#filter_statuses').val();
var filterOut = '';
if (status)
{
filterOut = 'variables.2eec46cc2f004d1023b0a55df699b65f=' + status; <!-- change the cat_item sys_id and variable.sys_id with actual sys id of catalog item and target variable. -->
var objFilter = {
id : 'custom_department_list_filter',
table : 'sc_req_item',
filter : filterOut
};
SNC.canvas.interactiveFilters.setDefaultValue({
id : objFilter.id,
filters : [objFilter]
}, false);
dashboardMessageHandler.publishFilter(objFilter.table, objFilter.filter);
}else{
dashboardMessageHandler.removeFilter();
}
}
filterStatus();
</script>

dohsan1
Tera Contributor

Hey Amit, your code fixed it, however, the "All" button doesn't reset the filters back to normal. Does anyone have a solution?

davidwarner007
Tera Contributor

To create a custom interactive filter based on catalog item variables in ServiceNow, you can follow these steps:

  1. Navigate to the Catalog Items section in the Service Catalog.

  2. Open the catalog item for which you want to create a custom interactive filter.

  3. Click on the "Variables" tab and select the variable that you want to use for the filter.

  4. Click on the "Variable Attributes" related link.

  5. In the Variable Attributes page, click on the "UI Properties" related link.

  6. In the UI Properties section, set the "Variable Type" to "Choice".

  7. In the "Choices" field, enter the values that you want to use for the filter options. Each value should be separated by a comma.......

  8. Save the variable and publish the catalog item.

  9. Navigate to the "Filters" section in the Service Catalog.

  10. Click on the "New" button to create a new filter.

  11. In the "Name" field, enter a name for the filter.

  12. In the "Table" field, select the catalog item table (sc_cat_item).

  13. In the "Advanced" section, click on the "Add Filter Condition" button.

  14. In the "Field" dropdown, select the variable that you want to use for the filter.

  15. In the "Operator" dropdown, select "is".

  16. In the "Value" field, enter the first value that you entered in the "Choices" field.

  17. Click on the "Add Or Condition" button.

  18. Repeat steps 14-17 for each value that you entered in the "Choices" field.

  19. Click on the "Save" button to save the filter.

  20. Test the filter by navigating to the Service Catalog and selecting the filter from the "Filter" dropdown.

 
 
 
kostyakozachuk
Tera Expert

This should solve resetting the filter when "All" option is chosen:

 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
    <g:evaluate var="jvar_status" object="true" jelly="true">
        var statusArray=[];
        <!-- Get choices of select box [status] and pass it to the widget UI -->
        var gr= new GlideRecord('question_choice');
        gr.addEncodedQuery('question=0956e00f1b29e51054eca752b24bcb36'); <!-- replace the sys_id with the sys_id of your variable. -->
        gr.addOrderBy('name');
        gr.query();
        while(gr.next()){
        statusArray.push([gr.getValue('value'),gr.getValue('text')]);
        }
        statusArray;
    </g:evaluate>
    <select id='filter_statuses' class='select2-search' onchange='filterStatus()'>
        <option value="">All</option>
        <j:forEach var="jvar_state" items="${jvar_status}">
            <option value="${jvar_state[0]}">${jvar_state[1]}</option>
        </j:forEach>
    </select>
    <script>
    var dashboardMessageHandler = new DashboardMessageHandler("filter_status");

    function filterStatus() {
        var status = $j('#filter_statuses').val();
        var filterOut = '';
        if (status) {
            filterOut = 'variables.0956e00f1b29e51054eca752b24bcb36=' + status; <!-- change the cat_item sys_id and variable.sys_id with actual sys id of catalog item and target variable. -->
            var objFilter = {
                id: dashboardMessageHandler._unique_id,
                table: 'sc_req_item',
                filter: filterOut
            };
            SNC.canvas.interactiveFilters.setDefaultValue({
                id: objFilter.id,
                filters: [objFilter]
            }, false);
            dashboardMessageHandler.publishFilter(objFilter.table, objFilter.filter);
        } else {
            SNC.canvas.interactiveFilters.removeDefaultValue(dashboardMessageHandler._unique_id, false);
            dashboardMessageHandler.removeFilter();
        }
    }
    filterStatus();
    </script>
</j:jelly>

 

Kumar116
Tera Contributor

How to apply it for multiple tables. for example, on sc_req_item and task_sla

Kumar116
Tera Contributor

Is it possible to apply this on dashboard where reports created on multiple tables such as sc_req_item, task_sla, sc_req_metrics, etc

Saurav Bhardwa2
Tera Contributor

@MrMuhammad 

There are two issues occurring:

  1. When I refresh the page in the report, it displays all the records initially. However, after making changes in the "Follow Interactive Filter" & "Show when Following Filter" fields, such as unchecking them and then refreshing again, the filter works as expected.

  2. Additionally, I am unable to find an option to navigate to the next set of records.

Could you confirm if this behavior is expected?

SauravBhardwa2_0-1709304684119.png

 

harsh tyagi1
Tera Explorer

hello I have reference field on RITM now I want a filter on the dashboard on the basic all the RITM report are visible let suppose there is 1 catalog item in this we  have a variable which is reference to incident table  so for this I create the reports and dashboard now I want a intractive filter  for the  reference variable, if user select incident on the basic all the reports of RITM will change  can any one help me to solve this problem

DJones4
Tera Explorer

Thank you for the great tutorial! In my case, the variable is a reference to department. I want to give the user a way to filter on these departments, however I can't seem to get that query to work. I'm building the list using the cmn_department table and setting the select option value to the sys_id, but the query:

 

'cat_item=d04b92181bd146503a6c2068b04bcb0c^variables.705e1e1c1bd146503a6c2068b04bcb90=' + dept
 
Doesn't filter the table at all. Is there something special I have to do for variables that reference type? 
 
Thanks!
 
EDIT: I seem to have resolved this by using `IN` instead of `=` in my query. Also, the code in this example seems to be missing an important detail about calling the `SNC.canvas.interactiveFilters.setDefaultValue()` function before `publishFilter()` - see here: https://docs.servicenow.com/bundle/tokyo-now-intelligence/page/use/dashboards/concept/c_CustomPublis...

 

Bongio
Tera Contributor

Hi,

So hepfull than you so much.

How can we modify the code to have the possibility to have muti select choice on the dynamic content please?

 

Thank you šŸ™‚

DJones4
Tera Explorer

@Saurav Bhardwa2 - this is a result of not calling `SNC.canvas.interactiveFilters.setDefaultValue()` first before `publishFilter()`. 

 

Here's an official example that shows how to do this: https://docs.servicenow.com/bundle/xanadu-now-intelligence/page/use/dashboards/reference/r_CustomPub...

 

@Bongio - this isn't too hard, where you are creating the `select` html element, you want to add the attribute `multiple` to the <select> tag. Then you would need to update the filter function so that it fetches all the values and builds a list, and then change the query to use `IN` instead of `=` such as `subcategoryINdb2,sql server,oracle`

 

Review this for the query operators:

https://docs.servicenow.com/bundle/xanadu-platform-user-interface/page/use/common-ui-elements/refere...

Daniel Bartholo
Tera Contributor

@DJones4 
Regarding the below would you mind sharing me your code on how you got the filter on Reference variable working to display the departments please? I would like to see how you used the 'IN' rather than the '=' and where. I am trying to do the same with a reference variable looking up cmn_locations. Right now the Dynamic Content simply displays All and not the referenced options.

 

In my case, the variable is a reference to department. I want to give the user a way to filter on these departments, however I can't seem to get that query to work. I'm building the list using the cmn_department table and setting the select option value to the sys_id, but the query:

 

'cat_item=d04b92181bd146503a6c2068b04bcb0c^variables.705e1e1c1bd146503a6c2068b04bcb90=' + dept
 
Doesn't filter the table at all. Is there something special I have to do for variables that reference type? 
 
Thanks!
 
EDIT: I seem to have resolved this by using `IN` instead of `=` in my query. 
etorices
Tera Contributor

Why, when I refresh the page, do I have to deactivate and save and activate again the follow interactive filter so that the filter works fine?

Version history
Last update:
ā€Ž11-21-2021 02:04 AM
Updated by: