UI Action Issue in Script - Return False

Joshua Cassity
Kilo Guru

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:

find_real_file.png

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:

find_real_file.png

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?

1 ACCEPTED SOLUTION

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.


View solution in original post

10 REPLIES 10

Abhinay Erra
Giga Sage

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