$npx -y skills add Impertio-Studio/Frappe_Claude_Skill_Package --skill frappe-syntax-hooks-eventsUse when implementing document lifecycle hooks via doc_events in hooks.py, understanding event execution order, or extending/overriding document behavior from another app. Prevents silent hook failures from wrong event names, incorrect execution order assumptions, and broken over
| 1 | # Document Lifecycle Hooks (doc_events) |
| 2 | |
| 3 | ## Quick Reference: Event Execution Order |
| 4 | |
| 5 | ### Insert (new document) |
| 6 | |
| 7 | | Order | Event | Purpose | Can Raise? | |
| 8 | |-------|---------------------|--------------------------------------|------------| |
| 9 | | 1 | `before_insert` | Set defaults before naming | YES | |
| 10 | | 2 | `before_naming` | Modify naming logic | YES | |
| 11 | | 3 | `autoname` | Set the `name` property | YES | |
| 12 | | 4 | `before_validate` | Auto-set missing values | YES | |
| 13 | | 5 | `validate` | Validation logic — throw to abort | YES | |
| 14 | | 6 | `before_save` | Final mutations before DB write | YES | |
| 15 | | 7 | `db_insert` | *Internal* — writes row to DB | — | |
| 16 | | 8 | `after_insert` | Post-insert logic (runs once ever) | YES | |
| 17 | | 9 | `on_update` | Post-save logic (runs on every save) | YES | |
| 18 | | 10 | `on_change` | Fires if any field value changed | YES | |
| 19 | |
| 20 | ### Save (existing document) |
| 21 | |
| 22 | | Order | Event | Purpose | |
| 23 | |-------|-------------------|-----------------------------------| |
| 24 | | 1 | `before_validate` | Auto-set missing values | |
| 25 | | 2 | `validate` | Validation logic — throw to abort | |
| 26 | | 3 | `before_save` | Final mutations before DB write | |
| 27 | | 4 | `db_update` | *Internal* — updates row in DB | |
| 28 | | 5 | `on_update` | Post-save logic | |
| 29 | | 6 | `on_change` | Fires if any field value changed | |
| 30 | |
| 31 | ### Submit |
| 32 | |
| 33 | | Order | Event | Purpose | |
| 34 | |-------|-------------------|------------------------------------| |
| 35 | | 1 | `before_validate` | Auto-set missing values | |
| 36 | | 2 | `validate` | Validation logic | |
| 37 | | 3 | `before_save` | Final mutations before DB write | |
| 38 | | 4 | `before_submit` | Pre-submit logic — throw to abort | |
| 39 | | 5 | `db_update` | *Internal* — updates row in DB | |
| 40 | | 6 | `on_submit` | Post-submit logic (GL entries etc) | |
| 41 | | 7 | `on_update` | Post-save logic | |
| 42 | | 8 | `on_change` | Fires if any field value changed | |
| 43 | |
| 44 | ### Cancel |
| 45 | |
| 46 | | Order | Event | Purpose | |
| 47 | |-------|-------------------|-------------------------------------| |
| 48 | | 1 | `before_cancel` | Pre-cancel validation | |
| 49 | | 2 | `db_update` | *Internal* — updates row in DB | |
| 50 | | 3 | `on_cancel` | Post-cancel logic (reverse GL etc) | |
| 51 | | 4 | `on_change` | Fires if any field value changed | |
| 52 | |
| 53 | ### Delete |
| 54 | |
| 55 | | Order | Event | Purpose | |
| 56 | |-------|----------------|--------------------------------| |
| 57 | | 1 | `on_trash` | Pre-delete cleanup | |
| 58 | | 2 | `after_delete` | Post-delete logic | |
| 59 | |
| 60 | ### Other Operations |
| 61 | |
| 62 | | Operation | Events (in order) | |
| 63 | |------------------------|----------------------------------------------------------| |
| 64 | | Rename | `before_rename` → `after_rename` | |
| 65 | | Amend | `before_insert` chain runs on the new amended doc | |
| 66 | | Update After Submit | `before_update_after_submit` → `db_update` → `on_update_after_submit` → `on_change` | |
| 67 | |
| 68 | --- |
| 69 | |
| 70 | ## doc_events in hooks.py: Syntax |
| 71 | |
| 72 | ### Basic Structure |
| 73 | |
| 74 | ```python |
| 75 | # hooks.py |
| 76 | doc_events = { |
| 77 | "Sales Invoice": { |
| 78 | "on_submit": "myapp.events.sales_invoice.on_submit", |
| 79 | "on_cancel": "myapp.events.sales_invoice.on_cancel", |
| 80 | }, |
| 81 | "Purchase Order": { |
| 82 | "validate": "myapp.events.purchase_order.validate", |
| 83 | } |
| 84 | } |
| 85 | ``` |
| 86 | |
| 87 | ### Wildcard: Apply to ALL DocTypes |
| 88 | |
| 89 | ```python |
| 90 | doc_events = { |
| 91 | "*": { |
| 92 | "after_insert": "myapp.events.global_handler.after_insert_all", |
| 93 | "on_update": "myapp.events.global_handler.track_changes", |
| 94 | } |
| 95 | } |
| 96 | ``` |
| 97 | |
| 98 | ALWAYS use `"*"` (string with a |