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

Hi Maria,



In the Script Include I posted, we only log the message "Assignment Group doesn't have either on-call schedule or Primary On-Call" if the MHSOnCall getPrimaryUserByGroup method did not return a user, or if the incident that is being looked-up doesn't exist.



At the time the UI Macro is clicked, has the Incident been submitted? By that, I mean is this an existing Incident, and you are clicking on the macro after loading the form? Or is it a New incident which hasn't been saved yet? The script above works only if the incident already exists.



It would be worthwhile to log what is returned by getPrimaryUserByGroup and whether the GlideRecord get operation succeeded. I have slightly modified the function to add that logging capability:



callGroupOne: function() {  


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


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


              var sys_id = this.getParameter("sysparm_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;  


              }  


  },




When you make the changes to your Scipt Include and try again, what do the log messages say?


Hi Cory,



Yes, I am clicking the UI macro on an existing incident that has been saved.



I made the changes on the script include, and these are the log messages:



find_real_file.png


Hi Maria,



Great! That means that we aren't getting the Incident record.



Let's change the client-side code. There is a line that attempts to get the record's sys_id:


ga.addParam("sysparm_sys_id", g_form.getValue("sys_id"));



Let's change that:


ga.addParam("sysparm_sys_id", g_form.getUniqueValue());



Also, on the Script Include, let's add a log line for the sys_id (after we get the value from sysparm_sys_id):


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



That way, we will know that the sys_id got passed up correctly.


I have a feeling that this will solve the issue.


Hi Cory,



I made the changes, so here are the script log statements:



find_real_file.png



I also got a message on the incident form stating "Workflow started." so I checked the workflow context, and it shows an error.  



find_real_file.png


Hi Maria,



That is telling us that the vars object wasn't passed in to the workflow properly, or it's not being referenced properly. That's odd, because lines 18 and 19 of the function I posted earlier specifically set up the vars object, give it the assignment group key, and the sys_id of the assignment group for that key.



Can you post the content of your Script Include, and the content of that "If" activity from your workflow?