The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Create interactive filter to search on string type field

Prabhh
Kilo Guru

Hello Team,

I am trying to add a filter for a string type field on the dashboard report table. I went through other community posts related to the same but didn't get much help on the code.

Please provide any example script to create a filter on string field.

Thank you in advance.

 

-Prabhh

1 ACCEPTED SOLUTION

Gabor10
Mega Guru

Hey everyone!

 

I had a really hard time finding any useful material on this topic as most of the links are dead.

 

The requirement I got from a user was that they should be able to filter their reports on their dashboards by the Short Description of the task.

 

I ended up creating a dynamic content block to act as an interactive filter. I hope somebody in the future finds this useful!

 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
 
	
	  <style>	  		  

</style>
	
 <script>
	 
     var my_dashboardMessageHandler = new DashboardMessageHandler("FilterShortDescription");
    
    function publishFilter (searchTerm) {
        var filter_message = {};
        filter_message.id = "FilterShortDescription";
        filter_message.table = "task";
	 
	 if (searchTerm == ""){
	 clearFilter();
	 } 
	 else {
	 filter_message.filter = "short_descriptionSTARTSWITH"+ searchTerm;
	 }
        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 = "FilterShortDescription";
        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="searchTerm" type="text" class="form-control" value="" onchange="publishFilter(this.value);"></input>
   
</j:jelly>

 

My end result looked something like this:

find_real_file.png

Ps.: I removed some parts of the sample script compared to what you can see on the screenshot. The sample script will only work with STARTSWITH search - so it should only act as a base which you can build upon.

You can add a couple of if conditions to make it "smarter", you may add your own CSS and HTML to the content block to make it work better with your own requirement. 

 

Have a nice day!

View solution in original post

43 REPLIES 43

Adam Stout
ServiceNow Employee
ServiceNow Employee

There was a session on this a K19.  If you attended, you can grab the presentation and lab on it. Essentially, you just need some UI that will build an encoded query string that you pass to the DashboardMessageHandler.  You can see an example in the docs of how this works if you search for Custom Interactive Filter.

Aaron Munoz
Tera Guru

I believe the lab Adam refers to is this one:

https://developer.servicenow.com/app.do#!/event/knowledge19/CCW1561

There is one activity called "Custom String Search Interactive Filter" with the code you can use for filtering on a string field. Does this solve your requirement?

Aaron, this is awesome. The activity looks like it would solve our requirement. The steps point to a table that appears to be home grown though. I didn't see a reference to how they created the Custom Dashboard Objects table. Any thoughts?

That was created to store the configuration of the custom filters.  Without that, you end up duplicating a lot of code which is a nightmare to maintain.