UI macros
UI macros are discrete scripted components administrators can add to the user interface.
UI macros are typically controls that provide inputs or information not provided by existing field types. By default, the system provides UI macros for a variety of user interface elements such as:
- All formatters
- The Service Catalog cart
- The action icons next to fields
- The action icons on forms and lists
- The widgets of a content management system
- The Orchestration activity designer
Administrators can create their own UI macros to provide custom controls or interfaces. Creating UI macros requires knowledge of Jelly script. Review the existing UI macros for examples and suggested approaches. Those who want to build custom interfaces with JavaScript technologies should consider Service Portal as an alternative.
Note: To view available UI macros, navigate to All > System UI > UI Macros.
Parent Topic:Creating custom UI Pages and UI macros
Related topics
Create a UI macro for a formatter
UI macro basics
UI macros can be used to build solutions that can’t be built using the available catalog variable types.
Accessible UI macros
Several UI macros are available in the UI macros [ui_macros] table. These UI macros have names starting with ui_ and emulate the behavior of a subset of standard form field types for use in UI pages. For example, the ui_date_time UI macro can act like a glide_date_time field in a UI page, providing a type-able input field with a date/time selector.
A UI macro can be included in a UI page with the <g:> Jelly tag. The following example includes the ui_date_time UI Macro, providing two optional attributes to the UI Macro:
<g:ui_date_time name="due_date" value="2023-11-24 08:30:00" onchange="checkDateValue()" />
The attribute values in the <g:> Jelly tag are provided to the UI macro as jvar-prefixed variables. The UI macro can use the jvar-prefixed variables in its XML.
| Attributes | jvar-prefixed variables |
|---|---|
| name | jvar_name |
| value | jvar_value |
| onchange | jvar_onchange |
Note: These UI macros aren’t intended to support all features of their corresponding form field type. In many cases, the macros are only intended for specific ServiceNow application use cases.
The ui_-prefixed UI macros include descriptions specifying supported attributes that can (or must) be provided in the UI page's <g:> Jelly tag. To view available UI macros, navigate to All > System UI > UI macros.
/
Custom UI macros
The ui_example UI macro uses three jvar-prefixed variables: jvar_name, jvar_test_attribute and jvar_laptop_type. These variables can be provided to the UI macro as name, test_attribute and laptop_type attributes in a <g:> Jelly tag in a UI page.
Creating the ui_example UI macro
Navigate to All > System UI > UI Macros and select New.
Name the macro
ui_exampleand add the following script to the XML field:<?xml version="1.0" encoding="utf-8" ?> <j:jelly trim="true" xmlns:j="jelly:core" xmlns:g="glide"> <div>name jvar: ${jvar_name}</div> <div>test_attribute jvar: ${jvar_test_attribute}</div> <div>laptop_type jvar: ${jvar_laptop_type}</div> </j:jelly>Using the macro in a UI page
Navigate to All > System UI > UI Pages and select New. You will be asked to select an administrator role.
Name the UI page and add the following script to the HTML field:
<?xml version="1.0" encoding="utf-8" ?> <j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null"> <div style="text-decoration:underline">Template include one:</div> <g:ui_example name="Fred Luddy" test_attribute="I am an attribute" laptop_type="ThinkPad" /> <hr/> <div style="text-decoration:underline">Template include two:</div> <g:ui_example name="Pat Casey" test_attribute="I am a different attribute" laptop_type="Macbook Pro" /> </j:jelly>Checking output
In the UI page, you can click Try It to view the results.
Note: If you create a macro and it doesn’t display as expected in the UI page, clearing the cache might help. To clear the cache of your instance, in your browser, enter <server_url>/cache.do.
Related topics
Calling UI macros
Administrators can call UI macros from certain record types associated with the user interface.
| Record type | Example | |||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Dictionary attribute | Display an icon for a reference field: ``` ref_contributions=ui_macro_name | |||||||||||||||||||||
| UI macro | Call a UI macro from another UI macro: ```
| |||||||||||||||||||||