A

n important part of managing the flow of any record submission is to be able to validate the information from the form or record and provide feedback to the end-user if there is something that they need to change. While there are many ways to validate different kinds of data, preventing the submission of that data is pretty straight-forward. In Service-now, there are two different ways to stop the submission of a record. One method works client-side (in the rendered html form) and the other method works in server-side javascript as the record hits the database.

Client-side abort:

Preventing client-side form submission is very simple. All you need to do is set up an ‘onSubmit’ client script and return ‘false’ in the script. Of course, you won’t simply want to return false, you’ll want to actually validate something first.

It has also been brought to my attention that simply returning false, while aborting the submission correctly, also allows users to navigate away from the form without checking for modified fields and confirming the navigation with users. This is known as the ‘dirty form’ capability in ServiceNow. The reason for this is that ‘dirty form’ checks for a ‘g_form.submitted’ option that gets set to true when the form is submitted. ‘dirty form’ needs this to be set to false so the solution is to set ‘g_form.submitted’ to false before aborting the submission. Thanks to Frantisek below for pointing this out!

Here’s a simple example that aborts submission of the user tries to submit a high-priority incident.

function onSubmit() {
   if(g_form.getValue('priority') == 1){
      alert('Record submission aborted.');

      //Make sure dirty form still works
      g_form.submitted = false;

      //Abort the submission
      return false;
   }
}



Server-side abort:

Server-side validation is also very simple. The primary difference with server-side validation is that it works in a business rule. Because of that, the syntax is slightly different. The most important part of this type of validation is that your business rule has a ‘When’ value of ‘Before’…so that your business rule runs before insert or update…otherwise the database action will have already taken place by the time your script runs.
Server-side aborts make use of the ‘setAbortAction’ method. SetAbortAction simply tells the system to abort the next database action (insert, update, or delete) for the record it is used against. Here’s an example script and screenshot of setAbortAction in a ‘Before’ business rule.

if(current.priority == 1){
   current.setAbortAction(true);
}



Whenever I use this method I always add another information message so it is clear to the user what happened.

if(current.priority == 1){
   gs.addInfoMessage('Record submission aborted.');
   current.setAbortAction(true);
}