5750 views

How to troublshoot UI Actions either or not showing or not working

 

Description

If a UI Action is not showing you can check the TableCondition, Visibility, and Action Name.

If a UI Action shows but does not work as expected when clicked check the Script.

 

Procedure

Table

UI Action can be defined for a specific table/database view, or by selecting Global it can be defined on every table. The UI Action also appears on tables that extend the selected table. You can change this behavior by overriding the UI Action - create a UI Action with the same action name for the extended table. If there is an action for the task table, then you need to create a new one for the Incident table with the same action name.

 

Condition

If the UI Action is not visible on the form, then the first step is to check the condition. 

A good way to easily determine if the condition is the cause of an action not showing is to make it blank and test again. Then if the UI Action shows you can determine what part of the condition was preventing the UI Action from showing.

 

The Condition field should contain an expression that evaluates to true or false. An empty Condition field evaluates to true, resulting in the UI Action to appear on the form. 

If the specified expression does not evaluate to true or false, then results can be unexpected -  the UI Action might or might not appear. It is best to use an 'if' condition or functions that evaluate to true or false.

The following is an example of a condition that returns an object, which is invalid:

(new GlideRecord(current.getTableName())) && gs.hasRole('catalog_admin')

Instead, a valid condition that returns true or false, would be:

(new GlideRecord(current.getTableName())).canWrite()

 

NOTE: For a list context menu UI Action, do not use the current object because it will be ignored. 

 

Visibility

On the UI Action form, there is a Related list "UI Action Visibility", you can include or exclude the View on the form that will show the action.

https://docs.servicenow.com/bundle/helsinki-platform-administration/page/administer/list-administration/concept/c_ControllingVisibilityWithRoles.html 

If there is at least one Include record then you must have an include for any other view you want the action to show. 

 

Action Name

The action name should be unique within UI actions unless you are overriding the specified action on the parent table. The action name can include '.' as a special character, however, this was restricted to '_' in earlier releases.

 

 


Script

If the UI Action is not working as expected when clicked, then it is most likely related to the script. In the script you can use 'current' which relates to the current form that you are on.

It is recommended that before you save the UI Action, you click on Check Syntax in the header of the Script field.

Variables should be defined before they are used, otherwise, an error is generated. To troubleshoot an action that is not working as expected, you can add log statements. Below are example of 2 different methods for logging statements, for client and for server side scripts. 

How a script is evaluated at execution time and what type of methods you can use, depends on whether the Client option is checked or not:

  • If not checked - then you can use server side methods such as gs... because they will be executed on a server.
  • If checked - then the OnClick field appears on the UI Action form and the script will be executed on the user's browser and on the server. In this case, use jslog(‘test’) for debugging, not gs.log(‘test’).

In the Onclick field you need to specify the name of a JavaScript function that will be declared in the Script field. The function will be executed when the button is clicked. Outside the function declaration, you can specify code that will always be executed when the form loads. So for client side, it is important to wrap the code in the function to ensure execution.

The following is an example of a script that contains both client and server side:

// OnClick field has value 'resolveIncident();' and Script Name 'resolve_incident'

function resolveIncident(){
//Set the 'Incident state' and 'State' values to 'Resolved'
g_form.setValue('incident_state', 6);
g_form.setValue('state', 6);


//Call the UI Action and skip the 'onclick' function
gsftSubmit(null, g_form.getFormElement(), 'resolve_incident'); //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')
serverResolve();

function serverResolve(){
current.incident_state = 6;
current.update();
}

 

Other fields

Other fields such as Form linkForm context menu and Form button also determine what type of action is created. 

 

 

Article Information

Last Updated:2018-04-11 09:34:14
Published:2018-04-11