Conditionally display a field decoration

amkatulak
Giga Expert

I modified the ui macro 'user_show_incs' to only show active incidents.   My question is, is it possible to only display this field ref decoration if there actually are active incidents for the caller on an incident?   It is misleading to our ServiceDesk to see the icon when there are no open incidents.   It looks like it might be possible with Fuji, but we are still on Eureka.

Thanks in advance!

1 ACCEPTED SOLUTION

amkatulak
Giga Expert

Thanks, I had to tweak a few things to get it to work, but overall a great help.


Here is my final ui macro (created new vs oob) and script include.


UI Macro:   user_show_active_incs


<?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_guid" expression="gs.generateGUID(this);" />


<j:set var="jvar_n" value="show_incidents_${jvar_guid}:${ref}"/>


<g:reference_decoration id="${jvar_n}" field="${ref}"


  onclick="showActiveRelatedList('${ref}'); "


  title="${gs.getMessage('Show active incidents')}" image="bell.pngx"/>




<script>


// show related list


// todo: should be part of the PopupWindow class


// todo: needs new stack name


function showActiveRelatedList(reference) {


  var s = reference.split('.');


  // Get the field name which is always last


  var referenceField = s[s.length - 1];


  var v = g_form.getValue(reference);


  var w = new GlideDialogWindow('show_list');


  w.setTitle('Related incidents');


  w.setPreference('table', 'incident_list');


  w.setPreference('sysparm_view', 'default');


  w.setPreference('sysparm_query', 'active=true^'+referenceField + '=' + v);


  w.render();


}



//begin adjustments to OOB






function checkDisplay${jvar_guid}(response){



//This function actually hides/displays the decoration depending on the parameter


//If we have no parameter or it is 0, hide, otherwise display


  var rowCount = (!response) ? 0 : parseInt(response.responseXML.documentElement.getAttribute('answer'), 10);


  var dis = (rowCount==0) ? 'none' : 'inline';


  gel('${jvar_n}').style.display = dis;


//if(rowCount!=0) gel('${jvar_n}').title = 'Show ' + rowCount + ' related records';


}





function decorationShow${jvar_guid}(element, original, changed, loading){


//This function is called by the onchange event


  if(changed!=''){


      var tbl = '${ref}'.split('.')[0];


      var field = '${ref}'.split('.')[1];


      var count = new GlideAjax('myScriptInclude');


      count.addParam('sysparm_name', 'ajax_getRowCount');


      count.addParam('sysparm_table', tbl);


      count.addParam('sysparm_query', 'active=true^' + field + '=' + changed);


      count.getXML(checkDisplay${jvar_guid});


  }else checkDisplay${jvar_guid}();


}


//Push the function into the event handler


var n = '${ref}'.replace(/\./g, '_');


var h = new GlideEventHandler('onChange_' + n, decorationShow${jvar_guid}, '${ref}');


g_event_handlers.push(h);




</script>


</j:jelly>


Script Include:


var myScriptInclude = Class.create();


myScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  ajax_getRowCount: function(){


  return this.getRowCount(this.getParameter('sysparm_table'), this.getParameter('sysparm_query'));


  },


  getRowCount: function(tbl, eq){


  /* @param tbl - string - table name


  * @param eq - string - encoded query string


  * @return integer - volume of records matching the criteria


  */


  var answer = 0;


  var agg = new GlideAggregate(tbl);


  agg.addEncodedQuery(eq);


  agg.addAggregate('COUNT');


  agg.query();


  answer = (agg.next()) ? agg.getAggregate('COUNT') : 0;


  return answer;


  },


  type: 'myScriptInclude'


});


View solution in original post

2 REPLIES 2

Anthony_vickery
Tera Expert

I would suggest creating your own custom macro, leaving the OOB macro untouched. You can then update the dictionary attributes to use your custom macro. You'll want to perform a glideaggregate query based on the current value of the user field. This will need to be performed if/when the field is changed, so you'll want to execute it via glideajax. It will run every form load and every change to the field. You can handle loading differently if required. I've created a reusable function to get the full row count in case you want to update the title or something like that, but you could adjust this to return true/false if the count is greater than 0.



The UI Macro (probably not spot on as I've grabbed the OOB 'show_user_incidents' macro from a Dublin instance) and modified it to suit this scenario. I haven't added much in the way of commenting, but hopefully enough to follow.


<?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_guid" expression="gs.generateGUID(this);" />


<j:set var="jvar_n" value="show_incidents_${jvar_guid}:${ref}"/>


<g:reference_decoration id="${jvar_n}" field="${ref}"


  onclick="showRelatedList('${ref}'); "


  title="${gs.getMessage('Show related incidents')}" image="images/icons/tasks.gifx"/>




<script>


// show related list


// todo: should be part of the PopupWindow class


// todo: needs new stack name


function showRelatedList(reference) {


  var s = reference.split('.');


  // Get the field name which is always last


  var referenceField = s[s.length - 1];


  var v = g_form.getValue(reference);


  var w = new GlideDialogWindow('show_list');


  w.setTitle('Related incidents');


  w.setPreference('table', 'incident_list');


  w.setPreference('sysparm_view', 'default');


  w.setPreference('sysparm_query', referenceField + '=' + v);


  w.render();


}



//begin adjustments to OOB






function checkDisplay${jvar_guid}(response){


//This function actually hides/displays the decoration depending on the parameter


//If we have no parameter or it is 0, hide, otherwise display


  var rowCount = (!response) ? 0 : parseInt(response.responseXML.documentElement.getAttribute('answer'), 10);


  var dis = (rowCount==0) ? 'none' : 'inline';


  gel('${jvar_n}').style.display = dis;


//if(rowCount!=0) gel('${jvar_n}').title = 'Show ' + rowCount + ' related records';


}





function decorationShow${jvar_guid}(element, original, changed, loading){


//This function is called by the onchange event


  if(changed!=''){


      var tbl = '${ref}'.split('.')[0];


      var field = '${ref}'.split('.')[1];


      var count = new GlideAjax('MyScriptInclude');


      count.addParam('sysparm_name', 'ajax_getRowCount');


      count.addParam('sysparm_table', tbl);


      count.addParam('sysparm_query', 'active=true^' + field + '=' + changed);


      count.getXML(checkDisplay${jvar_guid});


  }else checkDisplay${jvar_guid}();


}


//Push the function into the event handler


var n = '${ref}'.replace(/\./g, '_');


var h = new GlideEventHandler('onChange_' + n, decorationShow${jvar_guid}, '${ref}');


g_event_handlers.push(h);




</script>


</j:jelly>





Then the create a client callable script include with the following functions. I've called it 'myScriptInclude'


If you rename it, you'll need to update the glideajax call in the ui macro.


ajax_getRowCount: function(){


  return this.getRowCount(this.getParameter('sysparm_table'), this.getParameter('sysparm_query'));


}


getRowCount: function(tbl, eq){


/* @param tbl - string - table name


* @param eq - string - encoded query string


* @return integer - volume of records matching the criteria


*/


  var answer = 0;


  var agg = new GlideAggregate(tbl);


  agg.addEncodedQuery(eq);


  agg.addAggregate('COUNT');


  agg.query();


  answer = (agg.next()) ? agg.getAggregate('COUNT') : 0;


  return answer;


}


amkatulak
Giga Expert

Thanks, I had to tweak a few things to get it to work, but overall a great help.


Here is my final ui macro (created new vs oob) and script include.


UI Macro:   user_show_active_incs


<?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_guid" expression="gs.generateGUID(this);" />


<j:set var="jvar_n" value="show_incidents_${jvar_guid}:${ref}"/>


<g:reference_decoration id="${jvar_n}" field="${ref}"


  onclick="showActiveRelatedList('${ref}'); "


  title="${gs.getMessage('Show active incidents')}" image="bell.pngx"/>




<script>


// show related list


// todo: should be part of the PopupWindow class


// todo: needs new stack name


function showActiveRelatedList(reference) {


  var s = reference.split('.');


  // Get the field name which is always last


  var referenceField = s[s.length - 1];


  var v = g_form.getValue(reference);


  var w = new GlideDialogWindow('show_list');


  w.setTitle('Related incidents');


  w.setPreference('table', 'incident_list');


  w.setPreference('sysparm_view', 'default');


  w.setPreference('sysparm_query', 'active=true^'+referenceField + '=' + v);


  w.render();


}



//begin adjustments to OOB






function checkDisplay${jvar_guid}(response){



//This function actually hides/displays the decoration depending on the parameter


//If we have no parameter or it is 0, hide, otherwise display


  var rowCount = (!response) ? 0 : parseInt(response.responseXML.documentElement.getAttribute('answer'), 10);


  var dis = (rowCount==0) ? 'none' : 'inline';


  gel('${jvar_n}').style.display = dis;


//if(rowCount!=0) gel('${jvar_n}').title = 'Show ' + rowCount + ' related records';


}





function decorationShow${jvar_guid}(element, original, changed, loading){


//This function is called by the onchange event


  if(changed!=''){


      var tbl = '${ref}'.split('.')[0];


      var field = '${ref}'.split('.')[1];


      var count = new GlideAjax('myScriptInclude');


      count.addParam('sysparm_name', 'ajax_getRowCount');


      count.addParam('sysparm_table', tbl);


      count.addParam('sysparm_query', 'active=true^' + field + '=' + changed);


      count.getXML(checkDisplay${jvar_guid});


  }else checkDisplay${jvar_guid}();


}


//Push the function into the event handler


var n = '${ref}'.replace(/\./g, '_');


var h = new GlideEventHandler('onChange_' + n, decorationShow${jvar_guid}, '${ref}');


g_event_handlers.push(h);




</script>


</j:jelly>


Script Include:


var myScriptInclude = Class.create();


myScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  ajax_getRowCount: function(){


  return this.getRowCount(this.getParameter('sysparm_table'), this.getParameter('sysparm_query'));


  },


  getRowCount: function(tbl, eq){


  /* @param tbl - string - table name


  * @param eq - string - encoded query string


  * @return integer - volume of records matching the criteria


  */


  var answer = 0;


  var agg = new GlideAggregate(tbl);


  agg.addEncodedQuery(eq);


  agg.addAggregate('COUNT');


  agg.query();


  answer = (agg.next()) ? agg.getAggregate('COUNT') : 0;


  return answer;


  },


  type: 'myScriptInclude'


});