ui macro

Ak_12
Tera Contributor

can anyone help me with this script, my requirement is on incident form, i have create a icon near to the caller (reference field) clicking on that icon should popup with records of computer assigened to users selected in caller field

<?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" icon="icon-tree-right"/>
<g:inline template="list2_js_includes.xml"/>
<script>
// show related list
(function() {
function decorationShow(element, original, changed, loading) {
var $refButton = $j(gel('${jvar_n}'));
$refButton.attr('role', 'button');
}
var n = '${jvar_n}'.replace(/./g, '');
var h = new GlideEventHandler('onLoad' + n, decorationShow, '${ref}');
g_event_handlers.push(h);
})();
function showRelatedList(reference) {
try {
var displayValue = g_form.getDisplayBox(reference).value;
var title = new GwtMessage().getMessage("Showing records related to: {0} ", displayValue);
var s = reference.split('.');
var referenceField = s[s.length - 1];
var query = referenceField + '=' + g_form.getValue(reference);

var gdw = new GlideModal('show_list');
gdw.setTitle(title);
gdw.setSize(750);
gdw.setPreference('focusTrap', true);
gdw.setPreference('table', 'incident_list');
gdw.setPreference('sysparm_query', query);
gdw.setPreference('title', 'A New Title');
gdw.render();
} catch (e) {
jslog('error showing related list');
jslog(e);
}
}

</script>
</j:jelly>

2 ACCEPTED SOLUTIONS

Saurabh Gupta
Kilo Patron
Kilo Patron

Try below code

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<j:set var="jvar_n" value="user_show_assigned_ci_${ref}"/>
   <g2:evaluate var="jvar_show_cis_display" jelly="true">
      var id = __ref__.getSysIdValue();
      if (id == null)
         "none";
      else {
         var ga = new GlideAggregate('cmdb_ci');
         ga.addQuery('assigned_to', __ref__.getSysIdValue());
         ga.addAggregate("COUNT");
         ga.query();
         var total = 0;
         if (ga.next())
            total = ga.getAggregate("COUNT");
         if (total == 0)
            "none";
         else
            "";
      }
   </g2:evaluate>

   <a id="${jvar_n}" 
     onclick="showCIDialog('${ref}')"
     name="${jvar_n}" 
     style="display:$[jvar_show_cis_display]"
     title="${gs.getMessage('Show related configuration items')}">
     <img border="0" src="images/icons/cmbd_ci.gifx" />
   </a>

<script>
needsRefresh = false;
function onChange_caller_id_show_cis(element, original, changed, loading) {
   if (needsRefresh == false) {
      needsRefresh = true;
      return;
   }
   if (changed.length == 0) {
      $('${jvar_n}').hide();
      return;
   }
   var ga = new GlideAjax('ShowCallerCIsAjax');
   ga.addParam('sysparm_name', 'getAssignedCICount');
   ga.addParam('sysparm_user', g_form.getValue('${ref}'));
   ga.getXML(assignedCIReturn);
}

function assignedCIReturn(response) {
   var answer = parseInt(response.responseXML.documentElement.getAttribute("answer"));
   var e = $('${jvar_n}');
   if (answer > 0)
      e.show();
   else
      e.hide();
}

var h = new GlideEventHandler('onChange_incident_cmdb_ci_show_related', onChange_caller_id_show_cis, '${ref}');
g_event_handlers.push(h);

function showCIDialog(reference){
   var s = reference.split('.');
   var referenceField = s[1];
   var v = g_form.getValue(referenceField);
   var w = new GlideDialogWindow('show_list');
   w.setTitle("Caller's Configuration Item");
   w.setPreference('table', 'cmdb_ci_list');
   w.setPreference('sysparm_view', 'asset');
   w.setPreference('sysparm_query', "assigned_to=" + v);
   w.render();
}

</script>
</j:jelly>

Thanks and Regards,

Saurabh Gupta

View solution in original post

Script include as below and it should be client callable.

var ShowCallerCIsAjax = Class.create();

ShowCallerCIsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAssignedCICount : function() {
var user = this.getParameter("sysparm_user");
var gr1 = new GlideAggregate("cmdb_ci");
gr1.addQuery("assigned_to", user);
gr1.addAggregate('COUNT');
gr1.query();
gr1.next();
return gr1.getAggregate('COUNT');
}
});


Thanks and Regards,

Saurabh Gupta

View solution in original post

7 REPLIES 7

Script include as below and it should be client callable.

var ShowCallerCIsAjax = Class.create();

ShowCallerCIsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAssignedCICount : function() {
var user = this.getParameter("sysparm_user");
var gr1 = new GlideAggregate("cmdb_ci");
gr1.addQuery("assigned_to", user);
gr1.addAggregate('COUNT');
gr1.query();
gr1.next();
return gr1.getAggregate('COUNT');
}
});


Thanks and Regards,

Saurabh Gupta

Icon should be available in your instance path: https://instance.service-now.com/nav_to.do?uri=%2Fimage_picker.do%3Fsys_id%3D-1

images/icons/cmbd_ci.gifx

 

 


Thanks and Regards,

Saurabh Gupta

jaheerhattiwale
Mega Sage
Mega Sage

@Ak_12 Please check below thread. Similar question is already solved.

 

https://www.servicenow.com/community/developer-forum/create-a-ui-macro-button-on-incident-form/m-p/2...

 

Note: 

Please check the chat you will get whole idea as well as code.

 

Please mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023