OBLYTECH Logo
OBLYTECH Logo

UI Builder data resources, controllers and event handlers

A data resource in UI Builder fetches records, remote API results or client values and exposes them to the components on a page. Event handlers decide what happens when a user or the page acts. Controllers bundle data and event logic for component presets, and an event does nothing until it is mapped to a handler.

What a data resource does on a page

The data resource drawer is where a page’s data is defined. For the page these resources sit on, see UI Builder page explained. It lists the available resources, the configuration and events for the ones you have added, and a JSON preview of what each returned.

Resources fetch from Glide, GraphQL and REST APIs, then transform the result into a shape a component can read. The component reads a stable property path, so changing the source later means editing the resource rather than every component that displays its output. For the GraphQL side, see ServiceNow GraphQL API support.

Resource type What it does
Controller Bundles data and event logic and enables component presets
GraphQL Runs GraphQL queries and mutations
Transform Runs a script that reshapes input data into another format
Client state Holds client information, domain state, logic and user preferences
Composite Packages several resources into one reusable resource
REST Makes requests to REST APIs

Resources are grouped by application, so the set you see depends on the application you are in. The Global application holds the most common ones, while a workspace such as Customer Service Management exposes fewer. Only one GlideForm is supported per page.

Inherited and local resources

Inherited resources come from the surrounding frame or application, load automatically and are read-only in UI Builder. Local resources are the ones you add, usually for data the frame does not supply. A page can hold several local instances of one resource, such as two lookups with different settings.

Adding and configuring a data resource

The steps require the ui_builder_admin role:

  1. Open or create an experience, then create or open the page.
  2. Add a component first, because a resource needs something to bind to.
  3. Select the plus icon in the data resource drawer, choose Data resource, and pick one, such as Look up a single record.
  4. Select Add, then complete the required fields on the Configure tab.
  5. Expect errors in the configuration and preview panels until those fields are filled. UI Builder evaluates a resource as soon as it is added.
  6. Set a custom label and ID if the default is unclear. The resource then appears in the data and scripts drawer.
  7. Save from the UI Builder header.

Choosing when a resource loads

Each resource runs either immediately (eager), with the page or component, or only when invoked (explicit), after a button press or field entry. Eager loading suits core content, while explicit loading keeps the first load fast. A detail panel that opens after a click usually fits the explicit option. For wider sequencing advice, see UI Builder and workspaces best practices.

Binding data to components

Context binding reads URL parameters, so one page can show different records depending on the address. While you build, give the page a test value in the URL or a fixed value in its configuration. Data resource binding points at a resource’s output, such as a lookup’s display value placed into a button label. Component binding links one component to another, so a list menu’s selection can drive the rest of the page. Client state binding keeps a state value and a component property in step.

Open the data binding modal from the bind icon beside a field. The Data types tab lists data resources and client states, and the Formulas tab lists formulas. Double-click the data pill, then select Apply.

Controllers and component presets

A controller is a special kind of data resource. It holds data and event logic together so that a component preset can connect to it. The controller sends server data to the component and sends updated data back when the user interacts with it. The component library highlights which components have presets available.

There are two kinds. Data controllers decide what the page shows and can be added by hand. UI controllers come in only through page templates and cannot be added manually. Controllers that come with a template cannot be deleted from that page.

You can add several controllers to one page, but the same controller cannot be used twice. If a preset needs a controller you have not added, UI Builder prompts you to add it. In the current release, the record controller is the only controller that can be added to a page.

To add one, open the data resource drawer, select the plus icon, choose Data resource, search for form, select the controller and select Add. Then complete its three fields.

Field Purpose
Type Fixed to controller
Table The table the controller reads from
Sys ID The record identifier, or -1 to generate a new one

A Sys ID of -1 suits a record that does not exist yet. It generates a full GUID the page can use to store data against the record before the first save. One such use is saving attachments to that new record.

A preset maps component properties to controller outputs as paths beginning with the data prefix and the controller name. The data inspector shows a controller’s configuration and outputs. Preset event mappings cannot be edited, but you can add an extra handler under an event or disable the mapping from its modal.

Building a custom controller

A custom controller packages data, scripts and events for reuse across experiences. With the ui_builder_admin role, select Create, then Controller, and name it. Add and configure its data resources, then optionally add an external controller dependency, client state parameters and client scripts. Apply, save, and add the controller to a page.

Client state parameters and client scripts

A client state parameter stores a value that components can bind to, such as the count in the counter example. The counter shows the pattern: a count parameter of type Number with an initial value of 0, a text component bound to it, and two buttons whose Update client state parameter handlers use the Math formula ADD or SUB with 1. Interactive state usually takes this shape of value, binding and handler.

When declarative handlers are not enough, use a client script. Scripts are JavaScript written in the Now Code Editor. They can manipulate data, store results in client state, read data resource results, run a data resource operation and dispatch an event. Attach one from the component’s Events tab with Add a new event handler. For the general concept, see client script explained.

Events, mappings and handlers

Events are things that happen on a page, such as a click, a successful fetch or a radio button selection. Each component has its own events, and an event does nothing until a handler is mapped to it. The mapping passes the event’s payload or context to the handler that acts on it.

There are four event types: component, page, data resource and declarative action. Page events cover page and variant event mappings, dispatched events, which relay an event modeled on a parent handler’s payload, and handled events, which serve as source events for page event mappings.

Category Examples
Inherited Breadcrumb URL changed (link to destination), Add parameters to URL, Open or close modal dialog
Page-level Add, remove or clear alert notifications (info, warning, error); Set loading state; Update client state parameter; UXF macroponent viewport load requested
Data resource Operations such as Look Up Record, triggered by the resource’s own events
Client script A script that runs when the component’s event fires

Data resources raise three events: Operation Initiated, Operation Succeeded and Operation Failed. A reload button for a first load that fails and a success modal after a record is added are two uses. For other causes, see troubleshooting UI Builder and workspaces.

To map an event, open the component’s Events tab, choose Add event mapping, then pick the event and the handler, completing each with Continue. Configure the handler, select Add, save, and test with Preview. A list offers several events, such as Row clicked and Data fetch failed. Scripted navigation has its own page: UI Builder client script navigation.

A declarative action component must be bound to a handled event, and the Configure declarative action event mapping option adds the handler that defines the action.

Event payloads and generated handlers

An event payload is the data a component sends when an event fires, such as the event type, a timestamp, a user action or a sys_id. Payload options differ by component and event, and where one is unclear, a client script that logs event.payload to the console shows what is sent.

Now Assist can generate some handlers. Choose Link to destination in the handler picker, select Continue, then Get Started, and describe the action in the Generate configuration with Now Assist field. Accept or edit the result, select Add, and test with Preview. Now Assist can currently configure three handlers: link to destination, open or close modal, and view load requested.

To remove a handler, open the component’s Events tab, select the handler and choose Delete. The mapping it sat in is a separate object and needs its own deletion.

Frequently asked questions

Does a component need a controller?

Only when its preset depends on one. UI Builder asks you to add the controller when you select such a preset.

Why does a data resource show errors right after I add it?

UI Builder evaluates each resource as it is added. The errors stay until the required fields are filled in.

Why does my button do nothing?

An event does nothing until it is mapped to at least one handler. Add the mapping from the component’s Events tab.

Can one controller be used twice on a page?

No. Each controller can be used once per page, though several different controllers can be added.