Record lifecycle
What runs when a Record is saved, submitted, cancelled or amended.
Every write to a Record runs a fixed order of hooks inside one database transaction. The request or job owns that transaction, so record code never commits. Mail, realtime updates and jobs go out only after the commit.
Saving
A new Record runs these steps in order:
before_insert → naming → before_validate → validate → before_save
→ write (optimistic lock, child rows) → after_insert → on_update → on_changeA save of an existing Record skips before_insert, naming and after_insert.
The write checks modified. If someone else saved the Record after you opened it, your save fails with a 409 TimestampMismatch and the desk asks you to reload.
Docstatus
A RecType with is_submittable has three states.
docstatus | State | What you can do |
|---|---|---|
| 0 | Draft | Edit, save, submit, discard |
| 1 | Submitted | Edit only fields marked allow_on_submit, cancel |
| 2 | Cancelled | Amend. No other change |
A RecType that is not submittable keeps every Record at 0.
Each action
| Action | Moves | Hooks before the write | Hooks after the write |
|---|---|---|---|
| Insert | → 0 | before_insert, naming, before_validate, validate, before_save | after_insert, on_update, on_change |
| Save | 0 → 0 | before_validate, validate, before_save | on_update, on_change |
| Submit | 0 → 1 | before_validate, validate, before_submit | on_update, on_submit, on_change |
| Update after submit | 1 → 1 | before_update_after_submit | on_update_after_submit, on_change |
| Cancel | 1 → 2 | before_cancel | on_cancel, back-link check, on_change |
| Discard | 0 → 2 | before_discard | on_discard, on_change |
Any other move is refused with bad_docstatus. A cancelled Record takes no save, and a draft cannot be cancelled.
Cancel and links
Cancel checks for submitted Records that still link to this one. If any exist, the cancel fails with LinkExists and lists them, up to 20 per field. Cancel those first.
Sales Invoice SINV-0001 (submitted) → links to → Sales Order SO-0001
Cancel SO-0001 → refused: SINV-0001 links to itAmend
Amend copies a cancelled Record into a new draft with amended_from set to the source. By default the copy is named <source>-1, and an amendment of SO-0001-1 becomes SO-0001-2. Record Naming Settings can switch this to the RecType's normal naming rule. A cancelled Record can be amended only once.
SO-0001 (submitted) → Cancel → SO-0001 (cancelled) → Amend → SO-0001-1 (draft) → SubmitVersions
A RecType with track_changes writes a Version on every save that changes something. The form's timeline shows each change as Label: old → new.