Identifying UI Action Clicked (The Right Way)

///Identifying UI Action Clicked (The Right Way)

Identifying UI Action Clicked (The Right Way)

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!

By | 2018-07-09T15:00:04-06:00 December 2nd, 2010|Categories: Client scripts|Tags: , |6 Comments

About the Author:

6 Comments

  1. Hema September 23, 2014 at 12:20 am - Reply

    Hi Mark,

    I used the below line in my code.

    var action = g_form.getActionName();
    alert(action);

    and what i got is undefined in the alert.
    Due to this my code which is using the action is not getting proceeded. Is there any reason for the above script to retuen undefined value? Please advise.

    Thanks,
    Hema

    • Mark Stanger September 23, 2014 at 5:57 am - Reply

      I just tried the exact same thing on a SN demo instance and it worked fine. The only thing I can think of is that you’re not using it in an ‘onSubmit’ client script…which is pretty much the only place it would be applicable. If you’re using it in an ‘onSubmit’ client script, then you should try setting up a similar scenario in a clean demo instance to validate if the issue is your instance or not.

  2. Hema September 24, 2014 at 3:41 am - Reply

    Hi Mark,

    Yes, as you said I am using it in a OnSubmit client script only. And this particular function works perfectly for all other catalog items except for the one I am using. So it might not be a instance issue though. Please let me know if there is any other workaround for this.

    Thanks,
    Hema

    • Mark Stanger September 24, 2014 at 5:08 am - Reply

      If it works fine for all other catalog items, then you’ve probably got another client script or form element that’s interfering. I would check your browser console log for errors and see where that leads you.

  3. Jochen September 3, 2015 at 5:59 am - Reply

    action.getActionName() is no longer supported in Business Rules:
    “java.lang.SecurityException: Illegal access to method getActionName() in class com.glide.script.ActionDescriptor”

    • Mark Stanger September 3, 2015 at 6:58 am - Reply

      Thanks, I’ve removed it from the post above. Please let me know if you find another way to get that to work.

Leave A Comment