- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-22-2016 03:30 PM
I have been racking my brains out, how to create a UI Action, and have it set the work notes to mandatory A UI Policy will set it, but the state will have been updated by then. Not much use. I have been trying to use the article listed here, such as this one:
https://community.servicenow.com/thread/199189?q=cancel%20ui%20action%20mandatory
This is what I have based on the above link, almost verbatim, but when I hit the Button, nothing happens, no warning, no redirect, nothing. All I am trying to do is get the work notes to be mandatory, show a warning. and abort the UI action if there are none.
Many thanks, if I find a way I will post it here. Cheers, Mark S
Script |
---|
function cancelTask(){ if(g_form.getValue('work_notes') == ''){ g_form.hideFieldMsg('work_notes'); g_form.setMandatory('work_notes', true); g_form.showFieldMsg('work_notes','NOTES are mandatory when cancelling a change task.','error'); return false; //Abort submission }else { //do not enforce mandatory fields. gsftSubmit(null, g_form.getFormElement(), 'cancelTask'); //MUST call the 'Action name' } } if(typeof window == 'undefined') { cancelTsk(); } function cancelTsk(){ current.state=4; new Workflow().cancel(current); gs.addInfoMessage('You messed up!'); action.setRedirectURL(current); current.update(); action.setRedirectURL(current); } |
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-22-2016 03:35 PM
You are missing an "onclick" field in your UI Action form. when you click the "Client" checkbox, it is supposed to show that. You will need to make sure you get that added to your form, or figure out which UI Policy isn't working that is not showing that field. then you can add "cancelTask();" to that field once you get it to show up.
Or, you can edit the onclick field from your list view of UI Actions and enter the text above. That will tell it to call that function when clicked. I haven't looked at the rest of your code yet, but try that to see if it helps.
Not sure if it will conflict, but just in case, you may want to make your "action" name different than the client side function.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-22-2016 03:35 PM
You are missing an "onclick" field in your UI Action form. when you click the "Client" checkbox, it is supposed to show that. You will need to make sure you get that added to your form, or figure out which UI Policy isn't working that is not showing that field. then you can add "cancelTask();" to that field once you get it to show up.
Or, you can edit the onclick field from your list view of UI Actions and enter the text above. That will tell it to call that function when clicked. I haven't looked at the rest of your code yet, but try that to see if it helps.
Not sure if it will conflict, but just in case, you may want to make your "action" name different than the client side function.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-22-2016 05:36 PM
Hey Jonathan,
Looks like reply above did the trick. I will be posting the full context for a Cancel UI, along with a Business Rule to check for open tasks. The onClick field was on the form, just hidden away in my dev instance. So far looking good! Many thanks, Mark S.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-22-2016 06:26 PM
Awesome!
-Jon
Sent from my iPhone

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-22-2016 03:51 PM
Hi Mark,
Can you try with below script and let me know.
function cancelTask(){
if(g_form.getValue('work_notes') == ''){
g_form.hideFieldMsg('work_notes');
g_form.setMandatory('work_notes', true);
g_form.showFieldMsg('work_notes','NOTES are mandatory when cancelling a change task.','error');
return false; //Abort submission
}
//do not enforce mandatory fields.
gsftSubmit(null, g_form.getFormElement(), 'cancelTask'); //MUST call the 'Action name'
}
if(typeof window == 'undefined') {
cancelTsk();
}
function cancelTsk(){
new Workflow().cancel(current);
current.state=4;
current.update();
gs.addInfoMessage('You messed up!');
action.setRedirectURL(current);
action.setRedirectURL(current);
}
You may also find below link helpful for reference.
Client & Server Code in One UI Action - ServiceNow Guru