Legacy:Context Menu
From ServiceNow Wiki
Contents |
1 Overview
The Context Menu plugin moves context menus from UI Macros to the database. Each item in a context menu is a row in the Context Menu [sys_ui_context_menu] table. Customizations to the list context macros (context_list_header) from earlier versions are lost during upgrade and must be recreated manually in the Context Menu table.
2 Context Menus in Lists
The ServiceNow platform has three menus that are controlled by the Context Menu Plugin:
- List title: Click the title of the list to access options related to the entire list.
- List header: Right-click in the header row of a column to display actions related to that column.
- List row: Right-click in a row to see a menu with actions related to the values in that row.
3 Creating a Context Menu
To create a context menu, navigate to System UI > UI Context Menus, and then click New in the list of menus. The Context Menu form provides the following fields:
| Field | Description |
| Table | Select the table to which this context menu option is attached. The out-of-box menu items are attached to the Global [global] table, which applies the context menu option to all lists for all tables. If you specify a particular table, the option is available only on context menus in lists from that table. |
| Menu | Select the menu in which this option appears. See the sample lists on this page.
|
| Type | Select the type of menu option to create:
|
| Name | Type the label for the action as it will appear in the menu. |
| Parent | If this action is part of a submenu, type the name of the parent menu item. For example, in the out-of-box system, Personalize is a parent. |
| Order | Assign this item, menu, or separator an order number to determine where in the menu it appears. |
| Active | Enable or disable this context menu item. Only active items are shown in the context menu. |
| Condition | Create the conditions under which this menu option appears. For example, define the role that has permission to see this item. |
| Run onShow script | Select this check box to display the onShow script field. |
| Action script | The action script is the code that runs in response to the menu item that is selected. |
| Dynamic actions script | The dynamic actions script builds the dynamic items that appear on the menu, such as filters or views. |
| onShow script | The onShow script contains a script that runs before the menu is displayed that determines the items to present based on previous settings. |
A menu item for a dynamic action looks like this.
4 Available Variables
Use these variables to create scripts for context menus.
4.1 Action Script
The action script is the code that runs in response to the menu item being selected. This is a client-side script that runs in the user's browser. The following javascript variables are available to the Action script when it is executed:
| Variable | Description |
| g_list | GlideList2 against which the script runs. |
| g_fieldName | Name of the field against which the context menu runs. |
| g_fieldLabel | Label of the field against which the context menu runs. |
| g_sysId | The sys_id of the row or form against which the script runs. |
The out-of-box system uses the following in an action script to refresh the platform view:
g_list.refresh(1);
Another example is the use of these variables in a list header menu to sort a list by the selected field in descending order (z to a).
g_list.sortDescending(g_fieldName);
4.2 Dynamic Actions Script
The dynamic actions script builds the dynamic items that appear on the menu, such as filters or views. The following javascript variables are available to the Dynamic Action script when it is executed:
| Variable | Description |
| g_tableName | Name of the current table. |
| g_listId | ID of the list for which the context menu is built. |
| g_contextMenu.addAction(item_id, label, script_string, order) | Add options to the context menu and select the order in which they appear. |
The following example is used to display a list title menu item that controls the number of records displayed in the list view:
g_contextMenu.addAction('50', '50 rows per page', 'showRowsPerPage("50");', 50);
Note: The Action Script for this item must define the showRowsPerPage function so that when selecting this menu item, that function is called with an argument of "50".
4.3 onShow Script
The onShow script contains a script that runs before the menu is displayed that determines the items to present based on current information about the menu being displayed. The onShow script is typically used to change the menu items on the List Header Menu based on the current field column. The following javascript variables are available to the onShow script when it is executed:
| Variable | Description |
| g_menu | Context menu to be displayed. |
| g_item | Current context menu item. |
| g_list | GlideList2 against which the script runs. |
| g_fieldName | Name of the field against which the context menu runs. |
| g_fieldLabel | Label of the field against which the context menu runs. |
| g_sysId | The sys_id of the row or form against which the script runs. |
A example of an onShow script is one that determines when to enable or disable the Ungroup option in a list header menu based on whether the list is currently grouped or not.
if (g_list.getGroupBy()) { // list is grouped so enable to Ungroup menu item g_menu.setEnabled(g_item); } else { // list is not grouped, so disable the Ungroup menu item g_menu.setDisabled(g_item);
}



