- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2016 02:13 PM
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.
Thanks,
Maria
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2016 08:49 AM
After seeking help from our ServiceNow Technical Consultant, the UI macro is now working!
Here's the updated script include:
Here's the updated UI macro:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2016 05:26 PM
Hi Maria,
You are trying to mix server-side and client-side code together.
When this was a UI Action, the action would cause the form to submit, and included in the information on the form was which UI Action was used to trigger the submit. When the form data was received by the instance, it found the UI Action record that corresponds with the button that the user clicked, and then ran that code server-side.
In your macro you have defined a function to be called when the user clicks on the link, but that is going to run client-side. The code is going to execute within the user's browser, not within the instance. At best, it's going to throw errors into the client-side console log.
You need to implement a communication pathway between the server and the client- something that can tell the server "with regards to the record currently being viewed, please execute the specified function". This does exist, but it's going to take a bit more work to implement.
We have a client-side library called GlideAjax which is used for exactly this type of communication. You write your client-side code so that it calls GlideAjax, and GlideAjax tells the browser to send a request to the server, and then gets the response back from the server and lets your client-side code react to it. You also need a Script Include which is marked client-callable, which provides the server-side logic you want to call, and knows what parameters to expect and what to do with them.
You will want your Script Include to handle kicking off the workflow. It's going to need to have a function that uses the current record's sys_id and the sys_id of the on-call group. Something like this:
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");
var current = new GlideRecord("incident");
var onCall = new MHSOnCall("SMS", group1);
var user = onCall.getPrimaryUserByGroup();
if (user && current.get(sys_id)) {
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'
});
Your GlideAjax component will probably look something like this:
var ga = new GlideAjax("CallOnCallGroup");
ga.addParam("sysparm_name", "callGroupOne");
ga.addParam("sysparm_other_group", g_form.getValue("u_other_on_call_group_1"));
ga.addParam("sysparm_sys_id", g_form.getValue("sys_id"));
//tell the server we want to log this
ga.addParam("debug","true");
ga.getXMLAnswer(callback);
function callback(answer) {
if (answer)
g_form.addErrorMessage(answer);
else
g_form.addInfoMessage("On-call group has been called!");
}
In the context of your macro, the whole thing looks like:
<?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="callOnCallGroup1_${ref}"/>
<a id="${jvar_n}" onclick="callOnCallGroup1()" 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(){
var ga = new GlideAjax("CallOnCallGroup");
ga.addParam("sysparm_name", "callGroupOne");
ga.addParam("sysparm_other_group", g_form.getValue("u_other_on_call_group_1"));
ga.addParam("sysparm_sys_id", g_form.getValue("sys_id"));
//tell the server we want to log this
ga.addParam("debug","true");
ga.getXMLAnswer(callback);
function callback(answer) {
if (answer)
g_form.addErrorMessage(answer);
else
g_form.addInfoMessage("On-call group has been called!");
}
}
</script>
</j:jelly>
I hope this clarifies the issue. Please note that Script Include *must* be marked client-callable. Also, I took a guess that this was on the Incident form. If it's on some other form, make sure to substitute the correct table into the GlideRecord lookup. Finally, I did not test this at all, but the concept is more important than the execution.
Thanks,
Cory
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2016 10:05 AM
Thanks, Cory! You are correct that this is on the incident form.
I gave this a try, but for some reason, it's still not initiating the workflow (no call is being made). I made sure that the script include is "client-callable". Is there anything else I can check?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2016 10:20 AM
Hi Maria,
Are your debug statements showing up in the script log? These can be found in the Navigator under "Script Log Statements".
Are the values what you expect?
The startFlow() method will return a GlideRecord for the context which is being run. Try saving that into a variable and logging it's sys_id:
(replace line 31 in the Script Include):
var context = wf.startFlow(wfId, current, "update", vars);
gs.log("Context started: " + context.getValue("sys_id");
Does the context sys_id appear in the logs? If so, can you view the context by going to https://your-instance-name.service-now.com/wf_context.do?sys_id=context-sysid-here ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2016 01:21 PM