# Instructions for Fluent StateModel API
Always reference the StateModel API specifications for more details.
1. Import `StateModel` from `@servicenow/sdk/core`. One `StateModel()` call produces the model record plus **all** of its state, transition, condition, condition-field, and state-attribute rows — do not try to author those child tables separately.
2. Use a State Model to define a table's **state machine**: which states exist, which transitions between them are legal, and what must be true before a transition is allowed. Do not use it for field-level validation (that is `DataPolicy`/`UiPolicy`) or for orchestration (that is `Flow`).
3. `$id`, `name`, and `table` are mandatory. `table` drives **polymorphic model-table selection**: `change_request` → `chg_model`, `problem` → `prb_model`, `problem_task` → `prb_task_model`, and every other table → the base `sttrm_model`. Several properties are class-specific — `availableInUI`, `recordPreset`, `color`, and `itilChangeProcess` are `chg_model` only; `taskType` is `prb_task_model` only; `description` is stored only on the three subclass tables. Setting one on the wrong table emits a build hint and the value is silently dropped.
4. `states` is a map of **friendly keys to state configs**. The key is a local alias that `transitions` reference by name — it is never stored on the instance. `label` and `value` are mandatory; `value` is what lands in `stateField`. Mark exactly one state `initial: true` (a convention the build does **not** enforce, so zero or several initial states will compile).
5. Do not hardcode state `value`s from memory. Confirm the real choice values for the target table with `now-sdk query` against `sys_choice`, because OOB choices can be customized. As a starting point: change states run `'-5'` (New) through `'3'` (Closed); problem states run `'101'`–`'107'`; custom tables use whatever their `ChoiceColumn` defines.
6. Each `transitions` entry needs `$id`, `from`, and `to`, where `from`/`to` are **state keys** from `states`. Referencing a key that isn't declared is a hard build error and the transition is dropped. There is **no `name` property** on a transition — the build computes it.
7. **Gate transitions with `conditions`, never with a Business Rule calling `current.setAbortAction(true)`.** This is the most-violated rule in state-model authoring: an aborting Business Rule bypasses the model's own enforcement and error reporting.
8. Within a condition, `condition` (an encoded query) and `conditionScript` (a server-side script returning a boolean) are **mutually exclusive** — setting both is a hard build error. Prefer `Now.include('./cond.js')` for scripts. A condition script receives `current`, must return a boolean, and must **not** call `gs.addErrorMessage`/`gs.addInfoMessage`, because conditions are evaluated across REST, Flow, and UI channels.
9. `conditionType` and `attribute` are typed as open unions but **validated at build time**: pass an exact OOB name or a 32-character sys_id, or the build fails. OOB names are **case-sensitive** — note the lowercase `h` in `'Not On hold'`. `'Authorized'`, `'Not On hold'`, and `'Risk evaluation'` are `chg_model_condition_type` records whose semantics apply only to `change_request`. Use `conditionType: 'Mandatory Fields'` together with `fields: [...]` to require specific columns.
10. **Editing an out-of-box model:** `$id` is the sole identity mechanism at every level — there is no coalesce fallback. Set `$id` to the real OOB sys_id (found via `now-sdk query`) on the model and on each state/transition/condition you want to update in place; anything given a fresh `Now.ID[...]` is always **inserted** as a new record. That asymmetry is how you add a new state to an existing OOB model.
11. Beware the **clobber caveat**: every row you declare is fully re-written, and any field you omit is reset to its platform default — overwriting the OOB value. Restate the OOB values you want to preserve (the model's `availableInUI`/`defaultModel`, a state's `sequence`/`initial`, a transition's `automatic`), and declare only the rows you actually need so the blast radius stays small. Remove an OOB row with `Now.del('sttrm_state_transition', '<sys_id>')`.
12. For a **model-only update** (e.g. changing `recordPreset`), declare the model with its sys_id and omit both `states` and `transitions` — only that one row is written.
13. On tables **other than** `change_request`/`problem`/`problem_task` the model is inert until you wire it up twice: add a `ReferenceColumn({ referenceTable: 'sttrm_model' })` plus a `before/insert` `BusinessRule` that assigns the model, **and** add a generic `evaluateTransition` Business Rule to enforce conditions. Skipping either step is the most common reason a built state model never takes effect. In a scoped app that rule must be inlined as a string (not `Now.include`, which rejects the `global.` namespace), qualified as `global.STTRMModel`, and paired with cross-scope privileges (`write` on the target table, `read` on the four `sttrm_*` tables, `execute` on `STTRMModel`).
14. A `change_request` model with an `Assess` (`-4`) or `Authorize` (`-3`) state **must** ship a companion `Flow` that requests CAB approval, with its trigger scoped to that model (`chg_model=<sys_id>`). `StateModel` never sets `approval` itself, so the model alone leaves records stuck. Scope the trigger — an unscoped one fires for every change model on the table.
15. Skip redundant defaults (`active: true`, `sequence: 0`, `order: 100`) — they round-trip back as omitted. `StateModel` accepts `protectionPolicy` and `$override`, but **not** `$meta` (no `installMethod`, no `useEsLatest`), despite what the overview guide's list implies.
