Dynamic Content Block - Accessing a jelly variable

anfield
Tera Guru

 

Im running a custom script in a dynamic content block which ive adapted from the community here:

 

https://www.servicenow.com/community/platform-analytics-articles/dashboards-approaches-for-interacti...

 

Ive added my own code, so that it will return a list of users in the drop down from a certain group. From my second function I am looking to return an object (arrUserList) and pass that to elsewhere in the content block where it is needed - like in the URL above. I'm not familiar enough with how to do that in jelly scripting. Can anyone help? Ive confirmed that the data arrUserList looks correct.

 

Ive added comments where ive commented out the original code, and added two functions to replace that code (user_lookup and show users)

<?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 arrItemSysId            = [];
  var arrItemName             = [];
  var dashboardMessageHandler = new DashboardMessageHandler(
    "custom_watch_list_filter", 
    function() {
      resetCustomWatchListFilter()
    });
  
  
  // transform HTML selectbox to ServiceNow selection
  $j(document).ready(function() {
    $j("#myCustomInteractiveFilterChoice").select2();
  });  
 
  function rebuildCustomWatchListFilterUsers() {
    var arrOut = [];

    for (var numItemIndex = 0; numItemIndex &lt; arrItemSysId.length; numItemIndex++) {
      arrOut.push('&lt;li class="select2-search-choice"&gt;');
      arrOut.push('&lt;div&gt;');
      arrOut.push(arrItemName[numItemIndex]);
      arrOut.push('&lt;/div&gt;');
      arrOut.push('&lt;a href="#" onClick="removeCustomWatchListFilterItem(\'')
      arrOut.push(arrItemSysId[numItemIndex])
      arrOut.push('\');return(false)" role="button" class="select2-search-choice-close"&gt;&lt;/a&gt;');
      arrOut.push('&lt;/li&gt;');
    }

    $j("#myCustomInteractiveFilterSelectedItems ul:first").html(arrOut.join(''));
    $j('#myCustomInteractiveFilter span.select2-chosen').text(arrItemSysId.length == 0 ? 'ALL' : '');
  }
  
  function publishCustomWatchListFilter() {  
    var objFilter = {
      id     : 'custom_watch_list_filter',
      table  : 'dmn_demand',
      filter : arrItemSysId.length > 0 ? 'u_integration_developerISNOTEMPTY^u_integration_developerLIKE' + arrItemSysId : ''
    };
  
    SNC.canvas.interactiveFilters.setDefaultValue({
      id      : objFilter.id,
      filters : [objFilter]
    }, false);
  
    if (arrItemSysId.length > 0) {
      dashboardMessageHandler.publishFilter(objFilter.table, objFilter.filter);
    }
    else {
      dashboardMessageHandler.removeFilter();
    }
  }  
  
  function removeCustomWatchListFilterItem(strSysId) {
    if (typeof strSysId == 'string') {
      var numArrayIndex = arrItemSysId.indexOf(strSysId);
  
      if (numArrayIndex != -1) {
        arrItemSysId.splice(numArrayIndex, 1);
        arrItemName.splice(numArrayIndex, 1);
        rebuildCustomWatchListFilterUsers();
        publishCustomWatchListFilter();
      }
    }
  }
  
  function resetCustomWatchListFilter() {
    arrItemSysId = [];
    arrItemName  = [];
  
    rebuildCustomWatchListFilterUsers();
    publishCustomWatchListFilter();
  }
    
  function addCustomWatchListFilterItem(){
    var strSelectedValue = $j('#myCustomInteractiveFilterChoice option:selected').val();
    var strSelectedLabel = $j('#myCustomInteractiveFilterChoice option:selected').text();

    if (strSelectedValue == "all") {
      resetCustomWatchListFilter();
    }
    else {
      if (!arrItemSysId.includes(strSelectedValue)) {
        arrItemSysId.push(strSelectedValue);
        arrItemName.push(strSelectedLabel);
      }
  
      rebuildCustomWatchListFilterUsers();
      publishCustomWatchListFilter();  
    }
  }
</script>
<g:evaluate>
<! -- original code commented out
var arrUserList = [];
var grUser      = new GlideRecord('sys_user');

grUser.orderBy('name')
grUser.query();

while(grUser.next()) {
  arrUserList.push({
    name:  grUser.getValue('name'),
    sysId: grUser.getUniqueValue()
  });
}end original code commented out  -->  

function user_lookup() {
	var groupName = 'Integration';
	var group_sysid = 'fd398ab4db805380039a777a8c97778d';

    var usersysids = [];

    var groupGR = new GlideRecord('sys_user_group');
    //groupGR.addQuery('name', groupName);
	groupGR.addQuery('sys_id', group_sysid);
    groupGR.query();
    if (groupGR.next()) {
        var groupId = groupGR.sys_id.toString();

        var memberGR = new GlideRecord('sys_user_grmember');
        memberGR.addQuery('group', groupId);
        memberGR.query();
        while (memberGR.next()) {
            usersysids.push(memberGR.user.toString());
        }
	}
	return usersysids;
}

function show_users(){

	usersysids = user_lookup();
	
	var arrUserList = [];
	
	var usersysids = usersysids;
	var grc = new GlideRecord('sys_user'); 
	
	grc.addEncodedQuery('sys_idIN' + usersysids);
	grc.query();
	gs.log('GH user rows: ' + grc.getRowCount());
		while(grc.next()) {
			
			gs.log(" Name: " + grc.getValue('name') + "sys id: " + grc.getValue("sys_id"));
		arrUserList.push({
		name:  grc.getValue('name'),
		sysId: grc.getUniqueValue()
	  });
	} // end while
}	return arrUserList
show_users();	

	
</g:evaluate>   
<div class="widget-content" style="padding:10px" id='myCustomInteractiveFilter'>
<select style="width:100%;" id="myCustomInteractiveFilterChoice" class="select2-search" onchange="addCustomWatchListFilterItem();">
<div class="form-horizontal container-fluid">
  <option value="all">ALL</option>
  <j:forEach var="jvar_type" items="${arrUserList}">
    <g:evaluate var="jvar_type" jelly="true">
      var strName  = jelly.jvar_type.name;
      var strSysId = jelly.jvar_type.sysId;
    </g:evaluate>
    <option value="${strSysId}" label="${strName}">${strName}</option>
  </j:forEach>
</div>
</select>
<div id="myCustomInteractiveFilterSelectedItems" class="select2-container select2-container-multi interactive-filter__widget-content form_control" style="width: 100%;">
<ul class="select2-choices">
</ul>
	
	show_users();	
</div>
</div>
</j:jelly>

 

0 REPLIES 0