Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How do you create a searchbox (dynamic content) on a dashboard to perform a Keyword search to search for what is on the dashboard (a report that is looking at the sc_item_req table)

Jayvik
Tera Contributor

Hey, 

I need to create a searchbox (dynamic content item) on a dashboard to perform a Keyword search to search for what is on the dashboard (a report that is looking at the sc_item_req table).   

I saw someone creating a searchbox to look for CI's on an Incident table but I can't figure out how to do it for my scenario.  Any help is much appreciated 

1 ACCEPTED SOLUTION

Hello Jayvik,

Now that I'm at a computer here is the structure you would need to achieve your goal. 

Content Block named RITM Keyword Search:

<?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_mapping" object="true" jelly="true">
    var mapping = [{"table": "sc_req_item", "filterPrepend": "123TEXTQUERY321=", "filterAppend": ""}];
    JSON.stringify(mapping);
</g:evaluate>
<g:macro_invoke macro="ritm_keyword_search" name="StringFilterContentBlock" mapping="${jvar_mapping}" />
</j:jelly>

 

UI Macro named ritm_keyword_search:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<div id="${jvar_name}_if_display" class="form-horizontal" style="margin:0 10px 15px;">
<input id='${jvar_name}_if_text' class="form-control widget-content" value="" />
</div>
<div id="${jvar_name}_mapping" style="display: none">${jvar_mapping}</div>
<script>
	
    var tables${jvar_name} = JSON.parse($j("#${jvar_name}_mapping").text());
    // Widget information
    var if_obj${jvar_name} = $j("#${jvar_name}_if_display");
    
	//Generate and Set a DashboardHandler ID
    var if_widgetId${jvar_name} = if_obj${jvar_name}.closest(".grid-widget-content")[0].getAttribute("data-original-widget-sysid");
    
    var ${jvar_name}DMH = new DashboardMessageHandler(if_widgetId${jvar_name});
   //Set the text field
	var ${jvar_name}_text = $j('#${jvar_name}_if_text');
    
	// handle the field changes
    ${jvar_name}_text.change(function()
    {
        var filterObj = {id: if_widgetId${jvar_name}, filters: []};
        if (${jvar_name}_text.val().length > 0) {
            for(var t = 0; t &lt; tables${jvar_name}.length; t++)
            {
                
                filterObj.filters.push({
                    table: tables${jvar_name}[t].table,
                    filter: tables${jvar_name}[t].filterPrepend + ${jvar_name}_text.val() + tables${jvar_name}[t].filterAppend
                });
            }
            // add the filter
	        window.SNC.canvas.interactiveFilters.setDefaultValue({
				id: ${jvar_name}DMH, 
				filters: filterObj
				}, false);
            ${jvar_name}DMH.publishMessage(filterObj.filters);
        } else {
			 window.SNC.canvas.interactiveFilters.setDefaultValue({
				id: ${jvar_name}DMH, 
				filters: filterObj
				}, false)
            ${jvar_name}DMH.removeFilter();
        }
    });
</script>
</j:jelly>

 

Result:

find_real_file.png

Keyword on the ticket:

find_real_file.png


Please mark my answer as correct/helpful if it has helped you.

Thanks,
Logan

View solution in original post

4 REPLIES 4

Logan Poynter
Mega Sage

In my Github repository I have a content block and macro that should be exactly what you're looking for, I'm just not at a computer to give a full code snippet at the moment.

https://github.com/gh0stxplt/ServiceNow

 

Please mark helpful/correct if this has helped you. 

 

Thanks,

Logan

Hello Jayvik,

Now that I'm at a computer here is the structure you would need to achieve your goal. 

Content Block named RITM Keyword Search:

<?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_mapping" object="true" jelly="true">
    var mapping = [{"table": "sc_req_item", "filterPrepend": "123TEXTQUERY321=", "filterAppend": ""}];
    JSON.stringify(mapping);
</g:evaluate>
<g:macro_invoke macro="ritm_keyword_search" name="StringFilterContentBlock" mapping="${jvar_mapping}" />
</j:jelly>

 

UI Macro named ritm_keyword_search:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<div id="${jvar_name}_if_display" class="form-horizontal" style="margin:0 10px 15px;">
<input id='${jvar_name}_if_text' class="form-control widget-content" value="" />
</div>
<div id="${jvar_name}_mapping" style="display: none">${jvar_mapping}</div>
<script>
	
    var tables${jvar_name} = JSON.parse($j("#${jvar_name}_mapping").text());
    // Widget information
    var if_obj${jvar_name} = $j("#${jvar_name}_if_display");
    
	//Generate and Set a DashboardHandler ID
    var if_widgetId${jvar_name} = if_obj${jvar_name}.closest(".grid-widget-content")[0].getAttribute("data-original-widget-sysid");
    
    var ${jvar_name}DMH = new DashboardMessageHandler(if_widgetId${jvar_name});
   //Set the text field
	var ${jvar_name}_text = $j('#${jvar_name}_if_text');
    
	// handle the field changes
    ${jvar_name}_text.change(function()
    {
        var filterObj = {id: if_widgetId${jvar_name}, filters: []};
        if (${jvar_name}_text.val().length > 0) {
            for(var t = 0; t &lt; tables${jvar_name}.length; t++)
            {
                
                filterObj.filters.push({
                    table: tables${jvar_name}[t].table,
                    filter: tables${jvar_name}[t].filterPrepend + ${jvar_name}_text.val() + tables${jvar_name}[t].filterAppend
                });
            }
            // add the filter
	        window.SNC.canvas.interactiveFilters.setDefaultValue({
				id: ${jvar_name}DMH, 
				filters: filterObj
				}, false);
            ${jvar_name}DMH.publishMessage(filterObj.filters);
        } else {
			 window.SNC.canvas.interactiveFilters.setDefaultValue({
				id: ${jvar_name}DMH, 
				filters: filterObj
				}, false)
            ${jvar_name}DMH.removeFilter();
        }
    });
</script>
</j:jelly>

 

Result:

find_real_file.png

Keyword on the ticket:

find_real_file.png


Please mark my answer as correct/helpful if it has helped you.

Thanks,
Logan

Hey Logan!, 

This is great!!!, works like a charm.  Much appreciated!

Hi @Logan Poynter , I'm using keyword word search on two different fields (used two different content blocks). But it's working only if I remove either one of the content block on keyword search, how do I solve this?