OBLYTECH Logo
OBLYTECH Logo

g_list List View Client API in ServiceNow: Scripting Guide

GlideList2 is the client-side API that customizes a ServiceNow list view through the g_list variable. Scripts use it to add query terms, set ordering, grouping and paging, refresh the list, and read which rows are checked. Its methods act on the list itself, and the sections below take them method group by method group.

Reaching a list object with g_list

The g_list variable gives a script one list object at a time. For the list features these methods act on, see the list view guide. It is not available in the related lists form link UI action, but it is available in the lists form link UI action. The methods run in UI context menus and UI actions.

Call GlideList2.get with a DOM element ID or a list ID string. Both forms return null when no list matches, so test the result first:

var list = GlideList2.get(listId);
if (!list) return;
var orderBy = list.getOrderBy();

When a list script needs server data, it calls the server through GlideAjax, which runs server-side code from the client through a script include. For the wider client script model, see the client script explainer.

The 29 methods grouped by job

The table groups the methods by what they do to the list.

Group Methods What the group does
Object access get (DOM element), get (list ID) Return the GlideList2 object, or null
Query addFilter, setFilter, getQuery, getFixedQuery Add or set encoded query terms, and read the query back
Refresh refresh, refreshWithOrderBy, setFilterAndRefresh Redraw the list
Ordering setOrderBy, sort, sortDescending, getOrderBy Set and read the sort order
Grouping setGroupBy, getGroupBy Set and read the grouping field or fields
Paging setFirstRow, setRowsPerPage Set the first visible row and the rows per page
Visibility showHideList, showHideGroups, toggleList, toggleListNoPref Show, hide or toggle the list and its groups
State and identity getChecked, getListName, getTableName, getView, getTitle, getParentTable, getRelated, isUserList Read what the list is and what the user has selected or changed

Adding and reading query terms

The addFilter method takes one encoded query term and adds it to the existing query. The call g_list.addFilter(“active=true”) narrows the list to active rows. The term uses the standard Glide encoded query format. For the wider concept, see the encoded query explainer. For chained queries that run on the server, see the GlideQuery guide.

The setFilter method replaces the encoded query and ignores the ordering and grouping parts of the string. The setFilterAndRefresh method takes the same kind of string, keeps the ordering and grouping parts when they are present, and redraws the list in one call.

The getQuery method returns the encoded query. Its classic signature takes four optional Booleans in this order: orderBy, groupBy, fixed and all. The first three default to false, and all defaults to true, which includes the other three parts.

The getFixedQuery method returns the part of the query that a user cannot remove from the breadcrumb. Administrators set it with a sysparm_fixed_query parameter on the application module. A script that builds a larger query should prepend the fixed part so the restriction is kept.

Sorting, grouping and paging

The setOrderBy method accepts one field or several fields joined with the caret separator. Prefix a field with orderBy for ascending order or orderByDesc for descending order. Both prefixes are optional, and a field with no prefix takes the default order. The getOrderBy method returns the first ordering field, or an empty string when the list has no ordering.

The sort method sorts ascending on one field and sets it as the orderBy column. The sortDescending method sorts one field in descending order and sets it as the orderByDesc column.

The setGroupBy method sets the grouping for a single field. Setting an empty string and then refreshing removes the grouping.

The setFirstRow method sets the first row shown on the next refresh, and setRowsPerPage sets how many rows show on each page. A simple pager sets the first row, then moves it forward by the page size for each following call.

Refreshing: refresh or refreshWithOrderBy

The refresh method takes a first row number and an optional string of name-value pairs. Its first row defaults to the first row of the current view. The method ignores the orderBy part of the filter, so the list shows its natural order after the call.

The refreshWithOrderBy method takes the same first row argument and a description string, and it keeps the orderBy part of the filter when one is set. If a sort should still apply after the redraw, call refreshWithOrderBy.

Reading the state of a list

The getChecked method returns the sys_ids of the checked rows as one comma-separated string. It does not confirm that the current user can execute those records, so a script that acts on them should run its own access check first.

The getListName method returns the list name, which is usually the table name. The getTableName method returns the table name, getView returns the view used to display the list, and getTitle returns the list title.

For a related list, getParentTable returns the table the form is built on, and getRelated returns the field that connects the related list to the parent form. The isUserList method returns true when the user has changed the list layout through the list mechanic, so a script can branch on it before it changes the layout.

Showing, hiding and toggling the list

The showHideList and showHideGroups methods show or hide the list or its groups, and they save the collapsed or expanded state as a user preference. The toggleList method flips the list and also saves that state. The toggleListNoPref method flips the list without saving anything. Use toggleListNoPref when the change should not become the user’s stored choice.

Where list scripts run

Context menu action scripts run in the user’s browser and have four variables: g_list, the GlideList2 object; g_fieldName and g_fieldLabel, for the field the menu belongs to; and g_sysId, the sys_id of the row or form. A header menu can sort its own column with g_list.sortDescending(g_fieldName). For the menus themselves, see the list column and record context menus guide.

An onShow script runs before a context menu appears and uses g_menu and g_item to enable or disable entries. One example checks g_list.getGroupBy() and enables an Ungroup item when the list is grouped.

A dynamic actions script builds menu entries at display time, such as a set of filters or a choice of rows per page. It receives g_tableName, g_listId, g_itemName and g_itemOrder, and adds each entry with g_contextMenu.addAction, passing an item ID, a label, a script string and an order value.

In the Workspace Experience, a list button is a list action. An admin opens Workspace Experience, then Actions and Components, then List Actions, creates an action assignment, chooses Client Script, and adds GlideList calls into the onClick method. A button that uses getFixedQuery belongs under Related List Actions instead. A worked example adds a button that calls addFilter so the Incidents table shows active records.

How the Next Experience API differs

The GlideList (Next Experience) API is based on GlideList2 and uses the same g_list variable, built for the Next Experience UI framework. In the Australia release, each method must be used together with a refresh call, except sort and sortDescending.

Six GlideList2 methods are not supported there. In the Australia release, calling isUserList, setFirstRow, showHideGroups, showHideList, toggleList or toggleListNoPref writes an error to the console log. A script that runs in both interfaces should confirm which interface is active before it makes those calls.

The Next Experience getQuery method takes one optional object with orderBy, groupBy and fixed Boolean properties, instead of four positional Booleans. Its refresh and refreshWithOrderBy methods take just a first row argument.

What the list methods do not cover

The method lists for GlideList2 and the Next Experience API include no add or remove methods for a list-type field value, and no contains or toString methods. These classes work on the list view, so this guide does not show how to write items into a list-type field on a record.

Server-side field work uses other classes. In the Yokohama release, the server-side Glide class overview lists GlideRecord for database operations and GlideElement for operating on the fields of the current GlideRecord. Record-level scripting in general is covered in the GlideRecord explainer.

The GlideListV3 client API is deprecated and no longer supported. GlideList2 is the suggested replacement.

Common mistakes with the list object

Calling a method on the result of get without a null check can break a list script, because get returns null when no list matches.

Passing one object to getQuery in the classic API does not match its signature. The object form belongs to the Next Experience API.

Acting on the output of getChecked without an access check passes unverified sys_ids to later code.

Frequently asked questions

Can GlideList2 add or remove items in a list-type field on a record?

The class works on the list view. Its addFilter method adds a query term to the list, not an item to a field. Record-level field work uses the server-side GlideRecord and GlideElement classes.

Why does my refresh drop the sort?

The refresh method ignores the orderBy part of the filter, so the list uses its natural order. Use refreshWithOrderBy to keep the sort.

Will a collapse or expand choice be remembered?

The showHideGroups, showHideList and toggleList methods save the state as a user preference. The toggleListNoPref method changes the view without saving it.

Does this work in the Next Experience interface?

Several methods do. In the Australia release, six GlideList2 methods are not supported there, and calling them writes an error to the console log.

Is GlideListV3 still a valid choice?

No. It is deprecated and no longer supported, so new scripts should use GlideList2 in classic contexts or the Next Experience API where that interface applies.