# Instructions for Fluent UiMacro API

Always reference the UiMacro API specifications for more details.

1. Import `UiMacro` from `@servicenow/sdk/core` (SDK v4.14.0+). One call writes one `sys_ui_macro` record. Use it instead of `Record({ table: 'sys_ui_macro' })`, which still builds but writes only `name` and `xml`: no `scoped_name`, no `sys_name`, no defaults, and entity-escaped Jelly instead of CDATA.
2. Use a UI Macro for a reusable Jelly fragment invoked by tag from a UI Page, a form formatter, or a Service Catalog custom variable / custom cart. Prefer a built-in formatter (`Formatter.*` keys) over a custom formatter macro when one fits, and prefer a React/TypeScript `UiPage` for new full-page UI. Read the `jelly-guide` topic before writing non-trivial Jelly.
3. **`$id` is mandatory and is the identity.** There is no name-based coalesce, so renaming `name` updates the same record in place. Every `<g:...>` invocation of the old name must then be updated by hand.
4. **Write `name` without the scope prefix.** The build derives `scoped_name` (`<scope>_<name>` in a scoped app, empty in global). Never set `sys_name` or `scoped_name` yourself. A name that already carries the scope prefix produces a warning. A `<scope>_<name>` longer than 100 characters is a build error; in global scope the length is not checked, so keep names short there too.
5. **Invoke a scoped macro with the scope-qualified tag**: `<g:x_myapp_alert_banner />` for a macro named `alert_banner` in scope `x_myapp`, and `<g:alert_banner />` in global.
6. **Choose a distinctive name.** Two macros in one project with the same `name` and `mediaType` are a build error, reported on both. The platform also refuses an active macro whose `name` + `mediaType` match another active macro anywhere on the instance, in any scope. Only the install catches that, so query `sys_ui_macro` by name first (e.g. with this server's query_fluent_records tool).
7. **Keep the Jelly in its own file** with `xml: Now.include('./my-macro.jelly.xml')`. Inline `xml` in a template literal works, but every Jelly `${...}` must be escaped as `\${...}`. An unescaped one is a TypeScript interpolation and fails the build (TS2304). Phase-two `$[...]` needs no escaping.
8. **Write XML-safe Jelly.** Never place `&&` or `<` anywhere in an inline `<script>` body, comments included. Restructure instead: nested conditions, `>=` or descending loops, `b > a` rather than `a < b`. Raw characters produce only a malformed-XML build warning (the content is written unchanged) and fail at render time. The entity-escaped forms `&amp;&amp;` / `&lt;` pass the build but decode back to raw characters between the two Jelly passes, so they also fail at render time with "The content of elements must consist of well-formed character data or markup." That decode caveat applies only to `<script>` bodies. Attribute values are the opposite: the build does not check them, but they must still be XML-escaped — write `test="${a &amp;&amp; b}"`, never `test="${a && b}"`, or the platform's parser rejects the macro at render time.
9. **Respect the two Jelly phases.** Phase one (`j:`/`g:` tags, `${...}`) is compiled and cached. Phase two (`j2:`/`g2:` tags, `$[...]`) runs on every request. Put per-request data (current user, live counts) in `g2:evaluate` + `$[...]`. `dynamic="true"` is not a cache bypass.
10. **Document the parameters in `description`.** Macros have no typed parameter list. Callers pass attributes that arrive as `jvar_*` variables (`RP.getParameterValue()` does not read them), and `description` is the only parameter documentation other developers see. Treat every `jelly.jvar_*` value as untrusted input: never pass caller-supplied text into an evaluate block's query beyond table and field names.
11. **Omit `mediaType`** for standard macros and formatters. It is a device type/variant lookup key, not a MIME type. A value such as `'text/html'` produces a build warning, is still written, and is never matched at render time, so the macro silently fails to resolve.
12. `category` defaults to `'general'`; use `'catalog'` for catalog variable and cart macros. Any string is accepted (it is a platform choice field), and `''` clears a previously set category.
13. **Referencing a macro.** `UiMacro()` returns its config without a table brand. Pass the returned value directly only where the API's type lists `UiMacro`: `CatalogItem.customCart` and `CustomVariable` / `CustomWithLabelVariable` `macro` / `summaryMacro`. Everywhere else use `Now.ref('sys_ui_macro', '<$id key>')`, which resolves only within the current build.
14. **Custom form formatters.** Create the macro, then a `Record({ table: 'sys_ui_formatter' })` whose `formatter` is the macro's file name, and place it on a `Form` with `formatterRef` set to that record. In a scoped app the record's `formatter` must be the **scoped** file name `x_myapp_<name>.xml`; it is not prefixed for you, and an unprefixed value renders nothing with no build error. Omit `formatterName`: the Form derives it from the record's `formatter`, and a value you set overrides the record's with no diagnostic. A formatter macro renders inside the form's table, so it must emit `<tr><td colspan="2">…</td></tr>`, not a top-level `<div>`. See the `form-formatters-guide` topic.
15. In Service Catalog macros, scoped catalog client scripts run isolated (`window` is null), so never bridge between the macro and a client script through `window`.
16. **Match build diagnostics by message, not code.** The Fluent TSnn codes the build prints (e.g. TS11, TS213) are the TypeScript syntax kind of the flagged node, not stable error codes; genuine TypeScript errors such as TS2304 are.
17. `$meta` (`installMethod`, `useEsLatest`), `protectionPolicy` (`'read'` | `'protected'`) and `$override` are accepted. `useEsLatest` only emits an ES-latest script record (table sys_es_latest_script) for the macro; its runtime effect on Jelly `g:evaluate` blocks is not verified.
