Applying Multiple DashboardMessageHandler Filters from a single click

alondc
Mega Contributor

Hi Community, 


I have a pretty simple problem, I have reports from 3 tables (incident, sc_task, change_request) in a single dashboard. I want to create a dyanmic content block that will allow the user to select a customer, and then use that tenant's ID to publish a filter to all of the reports to only show info (for the INC's, tasks, and changes) for that customer. There is virtually zero documentation I have been able to find on how to deal with multiple mydashboardmessagehandlers other than vague allusions that it can be done. I have had mixed results with the content below so far. The first published filter works great, the second does not get applied at all, and the 3rd gets applied but the reports must be manually refreshed in the dashboard to display the updated value. 

 

NOTE: I have tried moving the order of the 3 publish filters around in the setfilter function. The issue persist with regards to where the argument is they all work if they are first, don't work if they are second, and work with a manual report refresh if they are 3rd. 

 

<?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 inc_dashboardMessageHandler = new DashboardMessageHandler("");
 var task_dashboardMessageHandler = new DashboardMessageHandler("");
 var chg_dashboardMessageHandler = new DashboardMessageHandler("");
 function setFilter()
	{
		var tenant_id = document.getElementById('tenant_id').value;
		inc_dashboardMessageHandler.publishFilter('sc_task', 'request_item.u_tenantLIKE' + tenant_id');
 		chg_dashboardMessageHandler.publishFilter('change_request', 'u_affected_accountsLIKE' + tenant_id); <!-- Unclear why but this middle filter never works   -->
		task_dashboardMessageHandler.publishFilter('sc_task', 'request_item.u_tenantLIKE' + tenant_id);	<!-- This filter is applied but the report does not auto-refresh with the new info   -->
	} 
function StopFilter()
	{
		task_dashboardMessageHandler.removeFilter();
		inc_dashboardMessageHandler.removeFilter();
		chg_dashboardMessageHandler.removeFilter();
	} 
</script>
<h1>Select the specific customer whose work you want to view.</h1> <br/> 
<p><strong> NOTE: This search will only display customers that are available in Service Now. </strong></p><br/> 
<g:ui_reference name="tenant_id" id="tenant_id" table="customer_account" selected="tenant_id" />
	<h2> Click on the Apply Filter once you have selected the customer </h2><br/>
<input id="all_tenants" type="button" value="Remove Filter" onclick="StopFilter();" />
<input id="only_selected_tenant" type="button" value="Apply Filter" onclick="setFilter()" />

</j:jelly>
1 ACCEPTED SOLUTION

Adam Stout
ServiceNow Employee
ServiceNow Employee

Try:

DMH.publishMessage([{"table": "incident", "filter": "state=1"}, {"table": "incident_task", "filter": "parent.state=1"}]);

I'm not sure if these query strings work, but hopefully, you get the point.

View solution in original post

14 REPLIES 14

Adam Stout
ServiceNow Employee
ServiceNow Employee

Try:

DMH.publishMessage([{"table": "incident", "filter": "state=1"}, {"table": "incident_task", "filter": "parent.state=1"}]);

I'm not sure if these query strings work, but hopefully, you get the point.

Thank you, that worked for me. However, the filter does not automatically apply when the forms reload. Have to apply it manually after the forms have loaded. Is there any solution for this?

Adam Stout
ServiceNow Employee
ServiceNow Employee

Persistence isn't supported with custom filters.  The best I have come up with is forcing a refresh on load of the widgets.  This isn't ideal, but it works.

@Adam Stout  - You are my hero, thank you so much!!!!


So I started experimenting with the publishMessage command to see how I could go about using it to apply filters to multiple tables using a value fetched from a UI field (I was having trouble apply that value in the publish message command but I knew I was almost there). Some googling of the command led my to this thread outlining a similar issue. Ramandeep Garg's answer was the key I was looking for into adding those values to the filters and applying them successfully.

 

I've pasted the excerpt of my (much simpler) code that is fully functional below. Thank you so much for your help, patience and guidance!!!


<?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 dmh = new DashboardMessageHandler("id1");
 function setFilter()
	{
		var tenant_id = document.getElementById('tenant_id').value;
		var inc_tenant = 'u_affected_accountsLIKE' + tenant_id;
		var task_tenant = 'request_item.u_tenant.sys_id=' + tenant_id;
		var chg_tenant = 'u_affected_accountsLIKE' + tenant_id;
		var inc_filter = dmh.getFilterMessage("incident", inc_tenant);
		var task_filter = dmh.getFilterMessage("sc_task", task_tenant);
		var chg_filter = dmh.getFilterMessage("change_request", chg_tenant);
		dmh.publishMessage([inc_filter,task_filter,chg_filter])
	} 
function StopFilter()
	{
	dmh.removeFilter();
	} 
</script>
<h1>Select the specific customer whose work you want to view.</h1> <br/> 
<p><strong> NOTE: This search will only display customers that are available in the Service Now. </strong></p><br/> 
<g:ui_reference name="tenant_id" id="tenant_id" table="customer_account" selected="tenant_id" />
	<h2> Click on the Apply Filter once you have selected the customer </h2><br/>
<input id="all_tenants" type="button" value="Remove Filter" onclick="StopFilter();" />
<input id="only_selected_tenant" type="button" value="Apply Filter" onclick="setFilter()" />

</j:jelly>

 

 

Hey Adam,

I followed the similar approach mentioned in the thread, but the interactive filter still doesn't seem to work. Could you please help me in understanding what's wrong with the below 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">
<script>
 var dmh = new DashboardMessageHandler("id1");
 function setFilter()
	{
		var tenant_id = document.getElementById('cfName').value;
		var obsFilter = "engagement.nameLIKE" + tenant_id;
		var dbViewFilter = 'en_nameLIKE' + tenant_id;
	
	dmh.publishMessage([{"table": "sn_audit_advanced_observation", "filter":obsFilter}, {"table": "sn_grc_qms", "filter":dbViewFilter }]);
		<!--dmh.publishMessage([inc_filter,task_filter])-->
	} 
function StopFilter()
	{
	dmh.removeFilter();
	} 
</script>

<input id="cfName" type="text" value="" />
<input id="all_tenants" type="button" value="Remove Filter" onclick="StopFilter();" />
<input id="only_selected_tenant" type="button" value="Apply Filter" onclick="setFilter()" />

</j:jelly>