home / skills / venkateshvenki404224 / frappe-apps-manager / frappe-state-machine-helper

frappe-state-machine-helper skill

/frappe-apps-manager/skills/frappe-state-machine-helper

This skill generates and validates Frappe DocType state machines, enabling robust workflow governance and safe, transparent document lifecycle transitions.

npx playbooks add skill venkateshvenki404224/frappe-apps-manager --skill frappe-state-machine-helper

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
2.8 KB
---
name: frappe-state-machine-helper
description: Generate state machine logic for Frappe DocTypes. Use when implementing complex status workflows, state transitions, or document lifecycle management.
---

# Frappe State Machine Helper

Generate state machine logic for managing complex document states and transitions in Frappe DocTypes.

## When to Use This Skill

Claude should invoke this skill when:
- User wants to implement document status management
- User needs state transition logic
- User mentions state machine, status workflow, or document lifecycle
- User wants to validate state transitions
- User needs state-dependent behavior

## Capabilities

### 1. State Transition Logic

**Order Status State Machine:**
```python
class SalesOrder(Document):
    def validate(self):
        self.validate_state_transition()

    def validate_state_transition(self):
        """Validate allowed state transitions"""
        if not self.is_new():
            old_status = frappe.db.get_value('Sales Order', self.name, 'status')

            # Define allowed transitions
            allowed_transitions = {
                'Draft': ['Pending', 'Cancelled'],
                'Pending': ['Confirmed', 'Cancelled'],
                'Confirmed': ['In Progress', 'Cancelled'],
                'In Progress': ['Completed', 'On Hold'],
                'On Hold': ['In Progress', 'Cancelled'],
                'Completed': [],  # Terminal state
                'Cancelled': []   # Terminal state
            }

            if old_status != self.status:
                allowed = allowed_transitions.get(old_status, [])
                if self.status not in allowed:
                    frappe.throw(
                        _(f'Cannot transition from {old_status} to {self.status}')
                    )

    def on_submit(self):
        self.status = 'Confirmed'

    def on_cancel(self):
        self.status = 'Cancelled'
```

### 2. State-Dependent Actions

**Actions Based on State:**
```python
class PaymentEntry(Document):
    def validate(self):
        if self.status == 'Draft':
            self.validate_draft_entry()
        elif self.status == 'Submitted':
            self.validate_submitted_entry()

    def before_submit(self):
        # Actions before state change
        if self.payment_type == 'Pay':
            self.validate_sufficient_balance()

        self.status = 'Submitted'

    def on_cancel(self):
        # Reverse actions
        if self.status == 'Submitted':
            self.reverse_gl_entries()

        self.status = 'Cancelled'
```

## References

**State Management Examples:**
- Sales Invoice: https://github.com/frappe/erpnext/blob/develop/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
- Purchase Order: https://github.com/frappe/erpnext/blob/develop/erpnext/buying/doctype/purchase_order/purchase_order.py

Overview

This skill generates state machine logic for Frappe DocTypes to manage complex document lifecycles, transitions, and status validations. It provides ready-to-use patterns for allowed transitions, state-dependent actions, and lifecycle hooks like validate, before_submit, on_submit, and on_cancel. Use it to enforce consistent workflows and prevent invalid state changes in your Frappe app.

How this skill works

The skill inspects requested DocType behavior and produces Python code that plugs into Frappe Document hooks (validate, before_submit, on_submit, on_cancel). It generates transition maps, validation routines that throw on illegal transitions, and state-dependent action blocks (e.g., reversing entries, pre-submit checks). The output is implementation-ready code snippets and guidance for integrating state checks with database-stored prior state values.

When to use it

  • Implementing complex status workflows for any DocType
  • Enforcing allowed transitions and preventing invalid state changes
  • Adding state-dependent pre- or post-actions (e.g., GL reversal, balance checks)
  • Validating document lifecycle during submit, cancel, or save
  • Designing terminal and non-terminal states in a business process

Best practices

  • Define a single allowed_transitions map per DocType and keep it explicit and small
  • Use validate() to compare current and stored status and frappe.throw() on violations
  • Update status in lifecycle hooks (before_submit, on_submit, on_cancel) to keep state changes atomic
  • Log and test terminal states (Completed, Cancelled) to avoid unintended further transitions
  • Keep side effects (GL entries, notifications) in dedicated methods and guard them by state checks

Example use cases

  • Sales Order workflow with Draft → Pending → Confirmed → In Progress → Completed/Cancelled
  • Payment entry flow that requires balance checks before submit and reverses GL entries on cancel
  • Purchase Order lifecycle enforcing approval gates and preventing direct jumps to Completed
  • Custom subscription DocType with On Hold and Resume transitions and billing actions on state change

FAQ

How do I prevent race conditions when validating state transitions?

Read the current status from the database (frappe.db.get_value) during validate() and perform checks before saving; use transactions or locking at the application level for concurrent updates if needed.

Where should I put complex side effects triggered by state changes?

Keep side effects in separate methods called from lifecycle hooks. Guard each method with explicit state checks to ensure they only run when the DocType is in the expected state.