I

‘ve seen a lot of requests on the forums asking how you can identify the button that got clicked form an onSubmit client script or a business rule. Usually the aim behind these questions is to make some field display or be mandatory based on a button click. While you can use client scripts and UI policy to do these things, there’s no built-in way to identify if a submit came from a particular UI Action. There is a way that’s been floating around for a while that you can do this and while it works, it’s not really the best way.

This post shows how to identify the UI Action that got clicked in an onSubmit script or business rule…the right way. In a single line of code you can return the Action name of the UI action and act on it accordingly.


You may have used code like this in the past to identify the UI Action that got clicked.

var formName = g_form.tableName + ".do";
//Get the form
var form = gel(formName);
var action = form.sys_action.value; //Get the 'Action name' value from the clicked UI Action
alert('You pressed ' + action);

Eric Jacobson from Service-now pointed out to me that all of this can be done in a single line of code. This method is better because it’s simpler and it uses a built-in API that’s less prone to break when you upgrade.

var action = g_form.getActionName();  //Get the 'Action name' value from the clicked UI Action

Here’s a practical example of where you might use this script. Suppose you had a UI Action button named ‘close_incident’ on your incident form that closed the incident record. Before closure, users should be required to enter ‘Close notes’ into the ticket. Normally this field is made mandatory based on a State change but users clicking the button might not have changed the state yet. This script identifies when the ‘close_incident’ button is clicked and makes the ‘Close notes’ field mandatory based on that click.

function onSubmit() {
   //Get the action value for the button clicked
   var action = g_form.getActionName();
   //If the 'close_incident' button was clicked and 'close_notes' field is empty
   if(action == 'close_incident' && g_form.getValue('close_notes') == ''){
      //Make 'close_notes' mandatory and abort the submission
      g_form.setMandatory('close_notes',true);
      return false;
   }
}

Identifying the action name in a business rule

This used to work using ‘action.getActionName()’ but ServiceNow has since removed the capability. Contact SN support if you’d like to see this return. If you know of something else that works in business rules please let me know and I’ll document it here!