The CreatorCon Call for Content is officially open! Get started here.

Dashboard Dynamic Content Block Interactive Filter Issue

SusanSchwar
Tera Contributor

I am using the old dashboard system. I created an interactive filter using a dynamic content block. The filter is to filter on a field that shows on the catalog item request form, not on the ritm. The filter works; however, it doesn't work on load. The widget it is affecting has to turn off it's "follow interactive filter" settings and then turn it back on every time a user accesses the dashboard. How to I change this so that the filter works automatically on load?

 

<?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=*enter sys_id*'); <!-- 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=*enter id*^variables.*enter id*=' + 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>
2 REPLIES 2

kaushal_snow
Mega Sage

Hi @SusanSchwar ,

 

You can Update your Dynamic Content Block Jelly script to include a delay and default-setting logic..

 

<?xml version="1.0" encoding="utf‑8"?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide">

  <g:evaluate var="jvar_status" object="true" jelly="true">
    var arr = [];
    new GlideRecord('question_choice')
      .addQuery('question', '<your variable sys_id>')
      .orderBy('name')
      .query()
      .forEach(gr => arr.push([gr.getValue('value'), gr.getDisplayValue('text')]));
    arr;
  </g:evaluate>

  <div class="widget-content">
    <select id="filter_statuses" onchange="applyStatusFilter()" style="padding: 2px;">
      <option value="">All</option>
      <j:forEach var="jvar_state" items="${jvar_status}">
        <option value="${jvar_state[0]}">${jvar_state[1]}</option>
      </j:forEach>
    </select>
  </div>

  <script>
    const FILTER_ID = 'filter_status';
    const TABLE = 'sc_req_item';
    const VAR_QUERY = 'cat_item=your_cat_item_sysid^variables.your_variable_sysid=';
    var dbh = new DashboardMessageHandler(FILTER_ID);

    function publishFilter(val) {
      const filter = val ? VAR_QUERY + encodeURIComponent(val) : '';
      SNC.canvas.interactiveFilters.setDefaultValue({
        id: FILTER_ID,
        filters: [{ id: FILTER_ID, table: TABLE, filter: filter }]
      }, true);
      filter ? dbh.publishFilter(TABLE, filter) : dbh.removeFilter();
    }

    function applyStatusFilter() {
      const val = gel('filter_statuses').value;
      publishFilter(val);
    }
    jQuery(() => {
      setTimeout(applyStatusFilter, 500);
    });
  </script>
</j:jelly>

 

Give it a try and let me know..

 

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/

Thank you for this, but the script comes back with an error: Error at line (7) Element type "question" must be followed by either attribute specifications, ">" or "/>".

 

I don't see such an error.