UI Macro button on a field

Maria DeLaCruz
Tera Guru

Hello,

I have a UI action that I want to convert to a UI Macro so that I can add it as a button at the end of a field.  

Here's my UI action:

var onCall = new MHSOnCall("SMS",current.u_other_on_call_group_1);

var user = onCall.getPrimaryUserByGroup();

if (user) {

      var wf = new Workflow();

  var   wfId = wf.getWorkflowFromName("MHS On-Call Voice");

  // add as many variables as your workflow is expecting, then pass the object

  gs.log ("Workflow ID "+wfId);

  var vars = {};

      vars.assignment_group = current.u_other_on_call_group_1;

  gs.log ("assignment_group in vars "+vars.assignment_group);

  gs.log ("vars [Object] "+vars);

  wf.startFlow(wfId, current, current.operation(),vars);

  action.setRedirectURL(current);

}

else {

  gs.addInfoMessage("Assignment Group doesn't have either on-call schedule or Primary On-Call ");

  action.setRedirectURL(current);

}

Here's what I've tried on the UI macro, but it's not working:

<?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="callOnCallGroup1_${ref}"/>

<a id="${jvar_n}" onclick="callOnCallGroup1('${ref}')" title="${gs.getMessage('Call On-Call Group 1')}" tabindex="0" class="icon ref-button icon-phone btn btn-default btn-ref"></a>

<script>

function callOnCallGroup1(reference){

var onCallgroup = g_form.getValue(reference.split('.')[1]);

var onCall = new MHSOnCall("SMS", onCallgroup);

var user = onCall.getPrimaryUserByGroup();

if (user) {

var wf = new Workflow();

  var   wfId = wf.getWorkflowFromName("MHS On-Call Voice");

  // add as many variables as your workflow is expecting, then pass the object

  gs.log ("Workflow ID "+wfId);

  var vars = {};

      vars.assignment_group = onCallgroup;

  gs.log ("assignment_group in vars "+vars.assignment_group);

  gs.log ("vars [Object] "+vars);

  wf.startFlow(wfId, current, current.operation(),vars);

  action.setRedirectURL(current);

}

else {

  gs.addInfoMessage("Assignment Group doesn't have either on-call schedule or Primary On-Call ");

  action.setRedirectURL(current);

}

}

</script>

</j:jelly>

The button shows on the field, but when I click it, nothing happens.   Please help.

find_real_file.png

Thanks,
Maria

1 ACCEPTED SOLUTION

Maria DeLaCruz
Tera Guru

After seeking help from our ServiceNow Technical Consultant, the UI macro is now working!



Here's the updated script include:



find_real_file.png



Here's the updated UI macro:



find_real_file.png


View solution in original post

15 REPLIES 15

Here's the script include:



var CallOnCallGroup = Class.create();  


CallOnCallGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {  


 


  callGroupOne: function() {  


              var debug = this.getParameter("sysparm_debug");  


              var group1 = this.getParameter("sysparm_other_group");  


              var sys_id = this.getParameter("sysparm_sys_id");  


      gs.log("The sys_id is: " + sys_id);


 


 


              var current = new GlideRecord("incident");  


      var gotIncident = current.get(sys_id);


 


              var onCall = new MHSOnCall("SMS", group1);  


              var user = onCall.getPrimaryUserByGroup();  


 


      gs.log("Did we get our incident? " + gotIncident);


      gs.log("Did we get our user? " + typeof user);


 


 


              if (user && gotIncident) {  


                      var wf = new Workflow();  


                      var wfId = wf.getWorkflowFromName("MHS On-Call Voice");  


                      var vars = {};  


                      vars.assignment_group = group1;  


         


                      if (debug == "true") {  


                              gs.log("Workflow ID " + wfId);  


                              gs.log ("assignment_group in vars " + vars.assignment_group);  


                              gs.log ("vars [Object] "+ new JSON().encode(vars));  


                      }  


         


  wf.startFlow(wfId, current, "update", vars);


     


  return "Workflow started.";  


 


 


              } else {  


                      gs.log("Assignment Group doesn't have either on-call schedule or Primary On-Call ");  


                      return;  


              }  


  },  


 


 


      type: 'CallOnCallGroup'  


});




Here's the content of the "If" activity in the workflow:



find_real_file.png



Hi Maria,



According to the wiki, to access the variables that are passed in from your script, you need to pull them off the workflow object:


Using Variables in a Workflow - ServiceNow Wiki



Is "assignment_group" one of the Inputs for this workflow? If so, then it seems like lines 6 and 7 in your Activity need to be updated to something like:


var escalationPlan = rota.getEscalationPlan(workflow.inputs.assignment_group, gdt);


gs.log('assignment_group ' + workflow.inputs.assignment_group);



Was this activity working previously, when this was being called by the old version of the macro (the script you posted originally)?


The UI macro never worked, but the script for the UI action I initially posted here works.


Hi Maria,



So far as I can tell, there are only two differences between how we are calling the workflow, and how the UI Action was calling the workflow:



1. We are passing in "update" directly instead of getting the value from "current.operation()". That should evaluate to "update" anyway.


2. We are passing in the sys_id of the assignment_group instead of the GlideElement attached to the GlideRecord for the incident.



Now, with the way that the Activity Script is currently set up, it is expecting a GlideElement (hence the dot-walk to the sys_id field and the explicit call to "toString()". That is easy enough to fix- we can either pass in "current.u_other_on_call_group_1" instead of the sys_id being passed up from the client-side, or we can update the Activity script to know that the value is already a string, and no dot-walking or toString()ing is necessary.



I am still not sure that would fix it- it seems like the error is telling us that the problem is "vars" is not a defined variable in the lexical scope of the activity script. From reading the wiki, it also seems like we would need to access the workflow object from within the activity script to get at the variables.



Lets go ahead and try both.



Firstly, let's replicate the way the UI Action was calling the workflow. We'll need to make a change to the Script Include:


Change these lines:


var vars = {};  


vars.assignment_group = group1;



To:


var vars = {};


vars.assignment_group = current.u_other_on_call_group_1;



That will tell the Script Include to use the actual GlideElement on the record as the "assignment_group" record.



Does that resolve the issue?


If so, you can clean up the Script Include by removing the group1 variable assignment (though you'll need to move the MHSOnCall stuff to after you verify that the incident has been loaded, and change the reference from group1 to current.getValue("u_other_on_call_group_1") when you do. You can also edit your client-side script to stop sending the sysparm_other_group value, since you won't be using it.



If that does not resolve the issue, then restore the Script Include lines as they were, and update the two Activity script lines as I mentioned previously.



If neither of those work, then I am fresh out of ideas. Id recommend opening a ticket in Hi so the Support team can help. Point them at this Community question for context, and they can help sort you out. It will be faster for them, since they will have access to get into your instance and reproduce directly. That can make troubleshooting and developing a fix much easier.



Thanks


Cory


Hi Cory,



I tried updating the script include and there's still an error in the "if" activity:



find_real_file.png


find_real_file.png



When I updated the 'if' activity in the workflow, the result returned 'no' like there's no schedule for the group, which is not true:



find_real_file.png find_real_file.png



I will open a ticket with HI regarding this.   Thanks so much for all your help.   I truly appreciate it!  



Maria