Hiding "Add Me" UI Macro when record is inactive

rlehmann
Kilo Sage

We've used http://www.servicenowguru.com/system-ui/ui-macros/add-me-ui-macro-user-group-fields/ to add the "Add Me" UI macro to the Assigned to field on task table records (Incident, Problem, Change, Catalog Task, etc.) to give people a quick way of assigning the ticket to themselves.

Previously we leveraged a client script to hide the macro from the form when the record is inactive.

To align with best practice, we are trying to retire any client scripts using DOM manipulation and update the macro.

I've attempted to tweak the macro to only work when the record is active.
The challenge is that in order for this version to work, we need to include the Active field on the form itself.
Preference is not to have the Active field visible on the form.

Is there another way we can achieve this without adding Active to the form?

<?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="add_me_${ref}"/>
   <a id="${jvar_n}" onclick="addMe('${ref}')">
     <img border="0" src="images/icons/user_obj.gifx" title="${gs.getMessage('Add me')}" alt="${gs.getMessage('Add me')}"/>
   </a>

   <g2:evaluate var="jvar_groups" expression="gs.getUser().getMyGroups()" />

   <script>
      function addMe(reference) {
         //Hide any field messages
         g_form.hideFieldMsg('assigned_to', true);

         //Check to see if the user is a member of selected group
         var aGrp = g_form.getValue('assignment_group');
         var myGroups = '$[jvar_groups]';
		 var active = g_form.getValue('active');

         if(active === 'true' $[AMP]$[AMP] aGrp $[AMP]$[AMP] myGroups.indexOf(aGrp) > -1){
            //Get the user reference field and populate the current user
            var s = reference.split('.');
            var referenceField = s[1];
            g_form.setValue(referenceField, '$[gs.getUserID()]');
         } else if(active !== 'true'){
			//Display a field error message
            g_form.showFieldMsg('assigned_to','Unable to update Assigned to. Record is no longer active.','error');
		 }
         else{
            //Display a field error message
            g_form.showFieldMsg('assigned_to','You are not a member of the current assignment group.  Please select one of your groups and try again.','error');
         }
      }
   </script>
</j:jelly>

Thanks in advance.

Ron

1 ACCEPTED SOLUTION

@rlehmann I have updated the code to remove the message and added a info message to check what value we are getting for active field from script include.

 

UI Macro:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
   
   <span id="add_me_button" onclick="addMe('${ref}')" title="Add me" alt="Add me" tabindex="0" class="btn btn-default icon-user-add">
      <span class="sr-only">Add me</span>
   </span>
 
   <g2:evaluate var="jvar_groups" expression="gs.getUser().getMyGroups()" />
 
   <script>
   setTimeout(function(){
   
   //Check if the record is Active
         var ga = new GlideAjax('ActiveState');
         ga.addParam('sysparm_name','taskActive');
         ga.addParam('sysparm_id', g_form.getUniqueValue()); // SYS ID of the record
         ga.getXMLAnswer(function(response){
        var active = response;
        g_form.addInfoMessage(active);
            if(active == 'true'){
                gel("add_me_button").style.display = "inline-flex";
            }
            else{
                gel("add_me_button").style.display = "none";
            }
        })
   
   }, 1000);
   
      function addMe(reference) {
         //Hide any field messages
         g_form.hideFieldMsg('assigned_to', true);
 
         //Check to see if the user is a member of selected group
         var aGrp = g_form.getValue('assignment_group');
         var myGroups = '$[jvar_groups]';
 
         if(aGrp){
            if(myGroups.indexOf(aGrp) > -1){
               //Get the user reference field and populate the current user
               var s = reference.split('.');
               var referenceField = s[1];
               g_form.setValue(referenceField, '$[gs.getUserID()]');
            }
            else{
               //Display a field error message
               g_form.showFieldMsg('assigned_to','You are not a member of the current assignment group.  Please select one of your groups and try again.','error');
            }
         }
         else{
            //Get the user reference field and populate the current user
            var s = reference.split('.');
            var referenceField = s[1];
            g_form.setValue(referenceField, '$[gs.getUserID()]');
         }
      }
   </script>
</j:jelly>
 
You can observe the below
jaheerhattiwale_0-1686198034415.png

 

Let me know if there is any issue.

 

If everything works fine then remove the info message from 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

View solution in original post

19 REPLIES 19

Thanks for the suggestion, this sounds like a viable option.
Scripting isn't my strongest skill, so I've tried the following without luck.
The UI Macro doesn't do anything now when clicked on either an active or inactive record.
Any suggestions on what may be missed?

 

Script Include (client callable):

var ActiveState = Class.create();
ActiveState.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	/* Check if the task is active */
	taskActive:function() {

		var taskRecord = this.getParameter('sysparm_id');
		var gr = new GlideRecord('task');
		gr.addQuery('sys_id', taskRecord);
		gr.query();
		if(gr.next()){
			return gr.active.getValue();
		}
		
	},
	
    type: 'ActiveState'
});

Updated UI Macro:

<?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="add_me_${ref}"/>
   <a id="${jvar_n}" onclick="addMe('${ref}')">
     <img border="0" src="images/icons/user_obj.gifx" title="${gs.getMessage('Add me')}" alt="${gs.getMessage('Add me')}"/>
   </a>

   <g2:evaluate var="jvar_groups" expression="gs.getUser().getMyGroups()" />

   <script>
      function addMe(reference) {
         //Hide any field messages
         g_form.hideFieldMsg('assigned_to', true);

       	 //Check if the record is Active
	     var ga = new GlideAjax('ActiveState');
		 ga.addParam('sysparm_name','taskActive');
		 ga.addParam('sysparm_id', ${ref}); // SYS ID of the record
		 ga.getXML(function(response)){
	     var active = response.responseXML.documentElement.getAttribute("answer");
	   //Check to see if the user is a member of selected group
         var aGrp = g_form.getValue('assignment_group');
         var myGroups = '$[jvar_groups]';


         if(active === 'true' $[AMP]$[AMP] aGrp $[AMP]$[AMP] myGroups.indexOf(aGrp) > -1){
            //Get the user reference field and populate the current user
            var s = reference.split('.');
            var referenceField = s[1];
            g_form.setValue(referenceField, '$[gs.getUserID()]');
         } else if(active !== 'true'){
			//Display a field error message
            g_form.showFieldMsg('assigned_to','Unable to update Assigned to. Record is no longer active.','error');
		 }
         else{
            //Display a field error message
            g_form.showFieldMsg('assigned_to','You are not a member of the current assignment group.  Please select one of your groups and try again.','error');
         }
      }
	   }
   </script>
</j:jelly>

 

@rlehmann Update the code like below and try again.

 

Script include:

var ActiveState = Class.create();
ActiveState.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    /* Check if the task is active */
    taskActive:function() {
        var taskRecord = this.getParameter('sysparm_id');
       
        var gr = new GlideRecord('task');
        gr.addQuery('sys_id', taskRecord);
        gr.query();
        if(gr.next()){
            return gr.active.toString();
        }

        return "false";
    },
   
    type: 'ActiveState'
});
 
UI Macro:
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
 
   <a id="add_me_button" onclick="addMe('${ref}')">
     <img border="0" src="images/icons/user_obj.gifx" title="${gs.getMessage('Add me')}" alt="${gs.getMessage('Add me')}"/>
   </a>

   <g2:evaluate var="jvar_groups" expression="gs.getUser().getMyGroups()" />

   <script>
   //Check if the record is Active
         var ga = new GlideAjax('ActiveState');
         ga.addParam('sysparm_name','taskActive');
         ga.addParam('sysparm_id', g_form.getUniqueValue()); // SYS ID of the record
         ga.getXMLAnswer(function(response){
            var active = response;
            if(active === 'true'){
                gel("add_me_button").style.display = "block";
            }
            else if(active !== 'true'){
                gel("add_me_button").style.display = "none";
                //Display a field error message
                g_form.showFieldMsg('assigned_to','Unable to update Assigned to. Record is no longer active.','error');
            }
            else{
                //Display a field error message
                g_form.showFieldMsg('assigned_to','You are not a member of the current assignment group.  Please select one of your groups and try again.','error');
            }
        })

      function addMe(reference) {
         //Hide any field messages
         g_form.hideFieldMsg('assigned_to', true);

       //Check to see if the user is a member of selected group
         var aGrp = g_form.getValue('assignment_group');
         var myGroups = '$[jvar_groups]';

         if(aGrp $[AMP]$[AMP] myGroups.indexOf(aGrp) > -1){
            //Get the user reference field and populate the current user
            var s = reference.split('.');
            var referenceField = s[1];
            g_form.setValue(referenceField, '$[gs.getUserID()]');
         }
       }
   </script>
</j:jelly>
 
 
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

@jaheerhattiwale Thanks very much for response.
Unfortunately, using the code you provided, it still allows the user to execute the UI macro on a closed, inactive ticket, setting themselves as the assigned to.

@rlehmann let me try it in my pdi and come back here, BTW how you have added the ui macro to form, can you please send the screenshot of that?

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

@rlehmann Tried in my PDI. 100% working solution.

 

Script include:

var ActiveState = Class.create();
ActiveState.prototype = Object.extendsObject(AbstractAjaxProcessor, {

/* Check if the task is active */
taskActive: function() {
var taskRecord = this.getParameter('sysparm_id');

var gr = new GlideRecord('task');
gr.addQuery('sys_id', taskRecord);
gr.query();
if (gr.next()) {
return gr.active.toString();
}

return "false";
},
type: 'ActiveState'
});

 

UI Macro:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
   
   <span id="add_me_button" onclick="addMe('${ref}')" title="Add me" alt="Add me" tabindex="0" class="btn btn-default icon-user-add">
      <span class="sr-only">Add me</span>
   </span>
 
   <g2:evaluate var="jvar_groups" expression="gs.getUser().getMyGroups()" />
 
   <script>
   setTimeout(function(){
   
   //Check if the record is Active
         var ga = new GlideAjax('ActiveState');
         ga.addParam('sysparm_name','taskActive');
         ga.addParam('sysparm_id', g_form.getUniqueValue()); // SYS ID of the record
         ga.getXMLAnswer(function(response){
  
            var active = response;
            if(active === 'true'){
                gel("add_me_button").style.display = "inline-flex";
            }
            else if(active !== 'true'){
                gel("add_me_button").style.display = "none";
                //Display a field error message
                g_form.showFieldMsg('assigned_to','Unable to update Assigned to. Record is no longer active.','error');
            }
        })
   
   }, 1000);
   
      function addMe(reference) {
         //Hide any field messages
         g_form.hideFieldMsg('assigned_to', true);
 
         //Check to see if the user is a member of selected group
         var aGrp = g_form.getValue('assignment_group');
         var myGroups = '$[jvar_groups]';
 
         if(aGrp){
            if(myGroups.indexOf(aGrp) > -1){
               //Get the user reference field and populate the current user
               var s = reference.split('.');
               var referenceField = s[1];
               g_form.setValue(referenceField, '$[gs.getUserID()]');
            }
            else{
               //Display a field error message
               g_form.showFieldMsg('assigned_to','You are not a member of the current assignment group.  Please select one of your groups and try again.','error');
            }
         }
         else{
            //Get the user reference field and populate the current user
            var s = reference.split('.');
            var referenceField = s[1];
            g_form.setValue(referenceField, '$[gs.getUserID()]');
         }
      }
   </script>
</j:jelly>
 
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