String Interactive filter

Promita Das1
Tera Contributor

Hi,

 

I want to create interactive filter for short description. It should display all the short description in drop down and when user select the short description then it related data should reflect in report.

 

Any idea how I can implement this requirement.

 

Thanks

Promita

5 REPLIES 5

Community Alums
Not applicable

Hi. This is a sample for the incident report.

 

Add Widget > Widget Category: Contents Blocks > *New Dynamic Content > Click "Add" button.
From "Click Here" link, move to content_block_programmatic form.
Copy this script to Dynamic content field and save.

<?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 = "incident";
		
		if (searchTerm == ""){
			 clearFilter();
		} 
		else {
			filter_message.filter = "short_description="+ 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 = "incident";
        filter_message.filter = "";
        SNC.canvas.interactiveFilters.setDefaultValue({
                id: filter_message.id,
                filters: [filter_message]
            }, false);
        my_dashboardMessageHandler.removeFilter();
    }
</script>   

<g:evaluate var="jvar_incident" object="true" jelly="true">
	var grInc = new GlideAggregate('incident');
	grInc.addAggregate('count'); //Count aggregate (only necessary for a count of items of each OS)
	grInc.orderByAggregate('count'); //Count aggregate ordering
	grInc.groupBy('short_description'); //Group aggregate by the 'short_description' field	
	grInc.query();	
	grInc;
</g:evaluate>
	
<select name="short_description" id="searchTerm" onchange="publishFilter(this.value);">
	<j:while test="${jvar_incident.next()}">
	<option value="${HTML:jvar_incident.getValue('short_description')}">${HTML:jvar_incident.getValue('short_description')}</option>

	</j:while>
</select>

</j:jelly>

 

Don't forget to check "Follow interactive filter" on the target report.

 

References:

https://docs.servicenow.com/en-US/bundle/tokyo-now-intelligence/page/use/dashboards/reference/r_Cust...

https://www.servicenow.com/community/platform-analytics-forum/create-interactive-filter-to-search-on...

https://servicenowguru.com/scripting/gliderecord-distinct-query/

 

Best regards,

Radek7
Mega Guru

This is not working, unfortunately.

Community Alums
Not applicable

Hi, @Radek7

 

The <g:evaluate> tags will allow you to run server-side code and access the usual server-side APIs, such as GlideAggregate.
In my PDI instance, this approach appears to work without any issues, so could you please try the same method and let me know the results?
If it still doesn't work, could you check if there are any error messages in the browser's developer tools or the server logs? 

スクリーンショット 2024-02-23 23.48.16.png

 

スクリーンショット 2024-02-23 23.48.39.png

 

Andrew_TND
Mega Sage
Mega Sage

Hi @Promita Das1 

This sounds like it would eat up alot of your memory a short description dropdown would be HUGE.

I can however give you some instructions on setting up a dynamic content block which will search parameters -  STARTSWITH on the short description if that helps? That way it shouldn't impact performance.

Create new dynamic content block and add this script

<?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("FilterSD");
    
    function publishFilter (searchTerm) {
        var filter_message = {};
        filter_message.id = "FilterSD";
        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 = "FilterSD";
        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>


Please mark as helpful or if its resolved the issue, CORRECT!