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

veena_kvkk88
Mega Guru

Hi Joshua, you are trying to write both client and server side script in one UI action. Please check out this article to do it the right way. It helped me a lot. Please ask if you have any questions.



Client & Server Code in One UI Action - ServiceNow Guru


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.


I tested this and when I click the button nothing happens at all.



Here is the behavior that I'm looking for (currently the Additional Comments (comments) field is hidden on form load using a UI Policy / Action):



1. User clicks the Cancel Request button.



2. The Additional Comments field (comments) is set to visible and mandatory. An alert or message is displayed informing the user that they need to enter in comments to be allowed to cancel the request.



3. User enters in the comments and clicks the Cancel Request button again.



        4a. If comments is not empty, set the state to 'closed_cancelled', set the stage to 'closed_complete', hide the comments field again, update the record and inform the user that it's been processed.



        4b.If comments is empty, inform the user that they must enter in comments to be allowed to cancel the request.



5. End of Process


Did you put the function call in the Onclick field?



Also, make sure you specify an action name for the UI action and put that in the gsftSubmit function call (in line 12).