OBLYTECH Logo
OBLYTECH Logo

ServiceNow UI Macro: Build, Call and Place One

A ServiceNow UI macro is a scripted component, written in Jelly, that adds a control or information to the user interface where a standard field type falls short. You create it under System UI, then call it from a UI page, another macro or a dictionary attribute, or place it on a form through a UI formatter.

What a UI macro does and where it fits

A UI macro is a named unit of interface code. Its record holds a name, a description and an XML field, and the XML holds the Jelly script that produces the output. That output is markup the browser renders, so a macro can draw a button, an input, a summary panel or a row on a form.

The usual reason to build one is an input or display that the existing field types do not offer. The platform uses macros itself in several places, including the Service Catalog cart, the action icons beside fields and the widgets of a content management system.

Macros suit the classic form and UI page layer. Teams building custom interfaces with JavaScript technologies can consider Service Portal instead, and that choice should be settled before any Jelly is written.

Check the built-in ui_ macros first

Review the macros the platform already ships under All > System UI > UI Macros. Those that imitate field-like controls carry names that start with ui_. The ui_date_time macro is the standard example: it gives a UI page a typeable date and time input with a selector.

Two cautions apply. These macros are not intended to support the full feature set of the field type they imitate, and many serve one application’s use case. Read the description on the macro record before reusing it, because it lists the attributes the tag accepts and which of them are required. For a different route to building interface pages, see UI Builder pages explained.

Creating a UI macro record

Creating a macro requires working knowledge of Jelly script. Macros that serve as formatter content need the ui_macro_admin role. Open All > System UI > UI Macros, select New and complete four fields.

Field What to enter
Name A unique, descriptive name. Macros that imitate field types follow the ui_ prefix.
Active Clear the check box to disable the macro without deleting its code.
Description The purpose of the macro and the parameters it expects.
XML The Jelly script that defines what the macro renders.

The platform includes a sample formatter macro. It draws a button labelled Attach file, and its script checks whether the current table name equals ni. On a match it calls the cart attachment save function with the sc_cart_item table; otherwise it calls the general attachment save function with sc_req_item. Treat the sample as a pattern and change the calls to suit the table your macro works with.

Passing values into a macro

When a page or another macro calls a macro with attributes, each attribute arrives as a variable with a jvar_ prefix. The attribute name becomes jvar_name, value becomes jvar_value and onchange becomes jvar_onchange. An attribute called laptop_type, for instance, arrives as jvar_laptop_type.

A worked example makes this concrete. Build a test macro called ui_example, then include it on a UI page. A macro called ui_example prints three jvar values inside div elements, and a UI page includes it twice with different attribute values. Clicking Try It on the UI page shows the rendered output.

Calling a macro from a page, a macro or a field

There are three routes, and each fits a different place.

Where the call sits How to write the call Example
UI page Use the macro invoke tag with a macro attribute A g:macro_invoke tag whose macro attribute holds the macro name
Another UI macro Use the macro name as the tag name A g:ui_macro_name tag inside the calling macro
Dictionary attribute Set the ref_contributions attribute to the macro name Displays an icon for a reference field

The invoke tag has a shortcut. When the tag name already includes the macro’s name, the macro attribute can be left out.

Placing a macro on a form with a UI formatter

A formatter is the form element that displays information which is not stored as a field on the record. Build the macro that defines the content first, then the formatter that refers to it, then add the formatter to the form.

The formatter procedure runs as follows:

  1. Open All > System UI > Formatters.
  2. Click New.
  3. In the Name field, enter a descriptive name for the formatter.
  4. In the Formatter field, enter the UI macro name with .xml appended.
  5. In the Table field, select the table where the formatter will appear.
  6. In the Type field, select Formatter.
  7. Click Submit.
  8. Open the form design for that table and add the formatter.

Creating the formatter requires the ui_macro_admin role. For the form layout that the formatter sits in, see form layout personalization and headers.

A formatter macro represents one row in the user interface, so its XML must begin and end with table row tags. In the Table field, select the table in which the formatter will be used.

Overriding a base formatter with a macro

The platform uses formatters for complex rendering on some base forms, such as the activity formatter that lists history on a task form, the process flow formatter that shows stages across the top of a record, and the approval summarizer at the bottom of an approval form.

A UI macro can override one of these. Open All > System UI > UI Macros, select New, and give the macro the same name as the formatter, leaving off the .xml extension. Complete the remaining fields and click Submit. This also requires the ui_macro_admin role. The base formatters themselves are covered in form formatters for activity and process flow.

Registering a macro against a UI extension point

Some applications expose UI extension points, which are named places where a custom macro can plug in without editing the application’s own code.

To register a macro, open All > System Extension Points > UI Extension Points and select the point. Click the Create Implementation related link, write the code in the UI Macro form, and click Update. The listing includes points predefined in applications such as Customer Service Management and Field Service Management.

Registering requires the application-specific developer role, the application-specific admin role or the admin role. Reviewing the resulting extension instances requires the admin role, or the developer or administrator role for that application.

An extension instance record holds four fields:

  • Point: the unique name of the extension point, up to 100 alphanumeric characters, including special characters.
  • Class: the UI macro that implements the point.
  • Application: the scope the point is assigned to. The system sets it and it cannot be changed.
  • Active: clear the check box to deactivate the instance.

On the application side, a point is placed in a macro with a call_extension tag that names the point and passes in the record the custom macro needs. For how these points fit into a wider build, see how custom code attaches to apps.

Jelly syntax that affects macro output

Jelly uses two tag namespaces. The j namespace holds the standard Jelly tags, such as j:if for a condition. The g namespace holds platform tags, such as g:evaluate, which runs server-side JavaScript and makes the result available as a variable. For the server-side record API, see GlideRecord in ServiceNow.

Check the result of any Jelly tag in a UI page before relying on it.

A j:if tag has no else branch, so use choose, when and otherwise for multi-branch logic.

The g:no_escape tag turns off automatic escaping, so if user input appears inside it the page opens a security vulnerability.

When a macro does not appear

Work through this checklist before assuming the macro is broken.

  • Confirm the Active check box is selected on the macro record.
  • Confirm the name in the tag, the invoke attribute or the formatter field matches the record exactly, including the ui_ prefix and the .xml suffix in a formatter’s Formatter field.
  • For a formatter, confirm the XML begins and ends with table row tags, and that the formatter has been added to the form design.
  • Clear the instance cache. If a macro does not display as expected, clearing the cache might help.

Frequently asked questions

What is a UI macro in ServiceNow? A scripted component, written in Jelly, that adds a control or piece of information to the user interface when the standard field types fall short.

Which role do I need to create a UI macro? Creating a formatter and overriding a base formatter require the ui_macro_admin role. Registering against an extension point uses the application-specific developer, application-specific admin or admin roles.

How do I put a UI macro on a form? Create the macro, then a formatter whose Formatter field is the macro name plus .xml and whose Type is Formatter. Submit it, then add the formatter to the form design.

Why does my macro not show up? Check that it is active, that its name matches exactly, and that a formatter has been added to the form design. Then clear the instance cache.