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.
//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.
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.
//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!
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
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.
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
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.
action.getActionName() is no longer supported in Business Rules:
“java.lang.SecurityException: Illegal access to method getActionName() in class com.glide.script.ActionDescriptor”
Thanks, I’ve removed it from the post above. Please let me know if you find another way to get that to work.