GlideForm & GlideList in ServiceNow: Client Script Guide
In the previous topic, we learned about client-side scripting and the main Glide classes used in ServiceNow. To understand how Glide classes work in different ways and processes, it is important to understand the different types in detail. This topic focuses on GlideForm and GlideList in ServiceNow.
GlideForm helps you control how a record form looks and behaves, while GlideList helps you work with lists of records. Understanding both will help you write better client scripts in ServiceNow.
What is GlideForm in ServiceNow?
GlideForm(g_form) is a client-side JavaScript API and global object that provides methods to customize record forms directly inside the user’s browser.
The g_form object is available automatically in supported client scripts, so you do not need to create it yourself.
Some important things to remember while using GlideForm are:
- GlideForm methods are meant for the client side only. Never use them in server-side scripts.
- Some methods may behave differently in Catalog Client Scripts or Wizard Client Scripts. Check the documentation for the specific method before using it.
- With g_form, you can read and set values, show or hide fields, make fields mandatory or read-only, show messages, change choice lists, and save the form.
GlideForm Methods
GlideForm methods in ServiceNow provide several ways to control form behavior. These client script form methods help you perform common tasks without writing everything from scratch.
The following table covers some of the most useful GlideForm methods in ServiceNow:
| Method | Purpose |
|---|---|
| getValue(‘field’) | Get the field’s stored value (the sys_id for reference fields) |
| setValue(‘field’, value, displayValue) | Set a field value (the display value is optional, for reference fields) |
| getReference(‘field’, callback) | Get the referenced record asynchronously (always use the callback) |
| setMandatory(‘field’, bool) | Make a field mandatory or not |
| setReadOnly(‘field’, bool) | Make a field read-only |
| addInfoMessage(msg) / addErrorMessage(msg) | Show a message at the top of the form |
| showFieldMsg(‘field’, msg, ‘info’/’error’/’warning’) | Show a message under a field |
| getUniqueValue() | Get the record’s sys_id |
What is GlideList (g_list)?
GlideList in ServiceNow is a JavaScript class used to manipulate and interact with lists on a web page. The g_list object provides methods for interacting with supported list interfaces. You can read the current filter, get selected record IDs, change list filters, and refresh a list.
For example, you can use GlideList when you want to filter an Incident list to show only active incidents.
GlideList Methods
Here is a GlideList ServiceNow most commonly used methods:
| Method | Purpose |
|---|---|
| getChecked() | Comma-separated sys_ids of the selected rows (key for list UI actions) |
| refresh(firstRow) | Reload the list |
Note: GlideList methods are not available in every ServiceNow interface or scripting context. Check the current API documentation before using them. Some methods and behaviors differ between classic interfaces and Next Experience.
GlideForm Vs GlideList
| Factors | GlideForm (g_form) | GlideList (g_list) |
|---|---|---|
| Target | The form of a single record | List fields, or lists of records |
| Used for | Reading and setting values, hiding fields, showing messages | Adding or removing list items, filtering or sorting a list |
| Where it runs | Form view (e.g., opening a specific incident) | List view (e.g., opening a specific incident) |
| Used in | Client Scripts, UI Policy scripts, client-side UI Actions | Client Scripts (list fields), list actions (list views) |
| Needs refresh() | No | Yes, in the Next Experience (except sort methods). |
| Example | g_form.setMandatory(‘work_notes’, true) | g_list.addFilter(‘active=true’) |
Practical Examples with GlideForm & GlideList
These GlideForm examples use the Incident table. Field names and values may differ in your instance, so test them in a personal developer instance first.
Example 1: Hide a field on a new record (OnLoad)
Javascript
function onLoad() {
if (g_form.isNewRecord()) {
g_form.setDisplay('close_notes', false);
}
}
Example 2: Make fields mandatory when an incident is resolved (OnChange)
Run this on the state field.
javascript
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if (newValue == '6') { // 6 = Resolved
g_form.setMandatory('close_code', true);
g_form.setMandatory('close_notes', true);
g_form.addInfoMessage('Please add the close code and close notes.');
} else {
g_form.setMandatory('close_code', false);
g_form.setMandatory('close_notes', false);
}
}
Example 3: Stop a save if data is missing (OnSubmit)
javascript
function onSubmit() {
if (g_form.getValue('state') == '6' && g_form.getValue('close_notes') == '') {
g_form.showFieldMsg('close_notes', 'Close notes are required to resolve this incident.', 'error');
return false; // stops the save
}
return true;
}
What’s Next?
After understanding the GlideForm and GlideList. In the next topic, we will understand GlideAjax, GlideUser and GlideDialogWindow in ServiceNow through a practical Client Script guide.
Next TopicNeed more support?
Get a head start with our FREE study notes!
Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.