- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2016 10:25 AM
I'm writing a UI Action button on our Request form to allow some folks to cancel the request if necessary. Here is my action setup and script below:
function cancelrequest(){
current.request_state = 'closed_cancelled';
current.stage = 'closed_complete';
g_form.setVisible('comments', false);
current.update();
}
if (current.comments == '') {
alert('Please add the reason for the cancellation into the Additional Comments field.');
g_form.setVisible('comments',true);
g_form.isMandatory('comments', true);
return false;
}
else {
cancelrequest();
}
For some reason I keep getting the following error upon save:
Any ideas here on what I'm doing wrong? I was under the impression that doing a "return false;" aborted the action. What am I missing here?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2016 10:38 AM
It would look something like this. (Untested code - Please verify)
Action name: cancel_request
Onclick: cancelRequest();
Script:
//Client-side 'onclick' function
function cancelRequest(){
if(g_form.getValue('comments') == ''){
//Remove any existing field message, set comments mandatory, and show a new field message
g_form.hideFieldMsg('comments');
g_form.setMandatory('comments', true);
g_form.showFieldMsg('comments','Please add the reason for the cancellation into the Additional Comments field.','error');
return false; //Abort submission
}
//Call the UI Action and skip the 'onclick' function
gsftSubmit(null, g_form.getFormElement(), 'cancel_request'); //MUST call the 'Action name' set in this UI Action
}
//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if(typeof window == 'undefined')
serverCancel();
function serverCancel(){
//Set the correct values
current.request_state = 'closed_cancelled';
current.stage = 'closed_complete';
current.update();
gs.addInfoMessage('Request' + current.number + ' cancelled.'); //replace number with your desired field.
action.setRedirectURL(current);
}
P. S. Please hit like, helpful or correct for my responses as they apply to you.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2016 10:30 AM
Looks like you want to write both client side and server side code in the UI cation. Your syntax is incorrect. Go thru this link
Client & Server Code in One UI Action - ServiceNow Guru