# Instructions for Fluent DatabaseView API

Always reference the DatabaseView API specifications for more details.

1. Import `DatabaseView` from `@servicenow/sdk/core` (SDK v4.13.0+). One call writes `sys_db_view` plus one `sys_db_view_table` per `tables` entry and one `sys_db_view_table_field` per `fields` entry — never author those child tables separately with `Record()`.
2. Use a database view for **read-only** reporting, lists, dashboards, or queries over data that already lives in two or more tables (e.g. incidents joined to their caller), including a LEFT OUTER JOIN for "records with no related X" reports. Prefer a view over creating a new `Table()` that stores a computed or aggregated copy of existing data, over a Script Include that aggregates `GlideRecord` queries, and over dot-walking once you need to group or aggregate by a related field.
3. Do **not** use a database view when data must be stored or written — use `Table()` (a view has no schema of its own and never accepts writes). Use `List()` instead for the list layout of a single table's own columns.
4. **`name` is the identity — a top-level `$id` is not accepted, and neither is `$meta`.** Both are rejected at compile time (TS2353), because the build coalesces `sys_db_view` on `name`. `protectionPolicy` and `$override` are accepted: the build copies `protectionPolicy` onto every child `sys_db_view_table` and `sys_db_view_table_field` row, while `$override` reaches the `sys_db_view` row only. Renaming a view changes its identity, so the build treats it as a new view.
5. **Supply an already-normalized `name`** — lowercase, with every character outside a-z, 0-9 and `_` replaced by `_`. A non-normalized name is a hard build error, because the platform rewrites the name on save and the next build would create a duplicate view instead of updating this one. The name must also be unique across all views and must not match a real table; the platform aborts the save, and the build does not check this.
6. **Prefix `name` with the scope:** `u_` in the global scope, `<scope>_` (e.g. `x_acme_`) in an `x_` scope. A missing prefix is a hard build error in both cases. Scopes that are neither global nor `x_`-prefixed (e.g. `sn_` or `now_` apps) are not prefix-checked at all.
7. Give **every** `tables` entry and **every** `fields` entry its own `$id` (`Now.ID[...]`). There is no natural key, because the same table can be joined twice under different prefixes; a missing `$id` fails to compile (TS2322) and the build cannot create the record. Use distinct `$id`s when the same field must appear twice under one table.
8. `variablePrefix` must contain no `_`, must be 30 characters or fewer, and must be unique within the view — all three are hard build errors. Use lowercase only (uppercase may prevent viewing the view in a list). A table's columns are then addressed as `<prefix>_<field>`, e.g. `inc_caller_id`.
9. Order the join explicitly: the lowest `order` (default `100`) is the base table, and later tables follow at `200`, `300`, and so on. Put the smallest or most selective table first and base predicates on indexed fields — performance degrades with every joined table. Two tables with the same `order` only emit a build hint that `now-sdk build` never prints, so set distinct values yourself.
10. `leftJoin: false` (default) is an INNER JOIN — only rows that match every joined table appear. Set `leftJoin: true` on a non-base table only when unmatched rows from the prior tables must still appear.
11. **`whereClause` is a raw join predicate, not an encoded query.** Write `<prefix>_<field>` names on both sides (e.g. inc_caller_id=usr_sys_id), combine conditions with `&&` and `||`, and use only the comparison operators = != < <= > >=. Never use encoded-query syntax ('^' for AND, '^OR' for OR), and never use `LIKE` or `CONTAINS` — they are not supported.
12. `fields` is optional; omit it on a table joined only to constrain the result through `whereClause`. But **once a table's `fields` list is non-empty it is an allowlist, including for the join engine**: every column of that table that any `whereClause` in the view references — including its own `sys_id` when another table joins against it — must be listed, or the join fails at runtime. The build does not check this.
13. **Table and field names are not validated at build time.** `table` accepts any string, and `field` is typed `TableSchemaDotWalk<T> | (string & {})` — the IDE autocompletes known columns, but a misspelled column or table still builds cleanly. Confirm names against `sys_dictionary` (e.g. with this server's query_fluent_records tool) before authoring. The type also autocompletes dot-walked paths, but neither the API doc nor the guide documents them for views — join the referenced table and expose its column instead.
14. A `fields` entry is only `$id` plus `field`. There is no alias, label, or per-field table; the view exposes each column as `<prefix>_<field>`, and that is the name consumers use.
15. For a calculated column, add a function field on top of the view: a `Table()` call with `augments` set to the view's `name`, whose schema `Column()` uses `functionDefinition` with `glidefunction:` syntax over prefixed names (e.g. glidefunction:concat(usr_name,' - ',inc_short_description)). See the `table-augments-guide` topic.
16. Feed dashboards from the view: in `Dashboard()`, set a data source's `tableOrViewName` to the view's `name`, and use prefixed names (e.g. `usr_name`) in `metrics` and `groupBy`. `DatabaseView()` returns its config, not a table-branded reference, so always reference a view by its `name` string. Prefer dashboards over classic `sys_report` records for new reporting.
17. Platform limits: a view cannot include a table that participates in table rotation (e.g. `ecc_queue`, `sys_history_set`, `sys_flow_log`; task-derived tables are never rotated), view tables are excluded from FTP exports, and they cannot be clone data preservers.
18. **Release-note corrections (SDK v4.13.0).** The release-note example does not compile against the installed package. Do not copy it:
    - There is no top-level `$id`.
    - Each joined table uses `table`, not "name".
    - The predicate key is `whereClause`, not "where".
    - Predicates join with underscores (inc_caller_id=usr_sys_id), not dots ("inc.caller_id=usr.sys_id").
    - Every `tables` entry needs `$id`.
    - There is no top-level "fields" array: `fields` lives inside each `tables` entry, and a field cannot be renamed (no "caller_name" alias).
    - "IDE autocomplete on table field references" is suggestion only (see 13).
    - The "app-prefix diagnostics" are real, hard build errors, but they apply only to the global and `x_` scopes (see 6).
    - The note names only `sys_db_view`, although the call also writes `sys_db_view_table` and `sys_db_view_table_field` rows.
19. Doc correction: the `database-view-guide` topic says its calculated-column section is followed by "the example below", but no such example exists. Follow instruction 15 instead.
