/frappe-apps-manager:frappe-syntax-serverscriptsfrom frappe-apps-manager
Guides writing Python code for ERPNext/Frappe Server Scripts (Document Events, API, Scheduler, Permission Queries). Prevents import errors by documenting sandbox rules and pre-loaded frappe.* methods.
Ships in frappe-apps-manager. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Server Scripts are Python scripts managed via Setup > Server Script in the Frappe/ERPNext UI. They run inside a RestrictedPython sandbox.
┌──────────────────────────────────────────────────────────────────┐
│ ALL import STATEMENTS ARE BLOCKED │
│ │
│ import json → ImportError: __import__ not found │
│ from datetime import * → ImportError: __import__ not found │
│ import frappe → ImportError (even frappe itself!) │
│ │
│ EVERYTHING you need is pre-loaded in the frappe namespace. │
│ NEVER write an import line. ALWAYS use frappe.utils.*, etc. │
└──────────────────────────────────────────────────────────────────┘
ALWAYS use the pre-loaded namespace instead of imports:
| Blocked import | Use instead |
|---|---|
import json | frappe.parse_json() / frappe.as_json() |
from datetime import date | frappe.utils.today() / frappe.utils.now_datetime() |
from frappe.utils import cint | frappe.utils.cint() (already loaded) |
import requests | frappe.make_get_request() / frappe.make_post_request() |
import re | Not available — restructure logic without regex |
import os / import sys | Not available — use a custom app instead |
# v14: enabled by default
# v15+: DISABLED by default — you MUST enable explicitly:
bench set-config -g server_script_enabled 1
# Or set server_script_enabled: true in site_config.json
NEVER expect Server Scripts to work on Frappe Cloud shared benches — they require a private bench.
| Type | Trigger | Key Variable |
|---|---|---|
| Document Event | Document lifecycle (save, submit, cancel) | doc |
| API | HTTP request to /api/method/{name} | frappe.form_dict |
| Scheduler Event | Cron schedule | (none) |
| Permission Query | Document list filtering | user, conditions |
CRITICAL: The UI names differ from internal hook names:
| Server Script UI | Internal Hook | Fires When |
|---|---|---|
| Before Insert | before_insert | Before new doc saved to DB |
| After Insert | after_insert | After first DB insert |
| Before Validate | before_validate | Before framework validation |
| Before Save | validate | Before save (new + update) |
| After Save | on_update | After successful save |
| Before Submit | before_submit | Before submit (docstatus 0→1) |
| After Submit | on_submit | After submit completes |
| Before Cancel | before_cancel | Before cancel (docstatus 1→2) |
| After Cancel | on_cancel | After cancel completes |
| Before Delete | on_trash | Before permanent delete |
| After Delete | after_delete | After permanent delete |
NEVER confuse "Before Save" with before_save — the UI label "Before Save"
maps to the validate hook. The actual before_save hook runs AFTER validate.
Need custom Python logic for a DocType?
│
├─► Can you install a custom Frappe app?
│ ├─► YES: Use a Document Controller when you need:
│ │ • import statements (any Python library)
│ │ • File system access
│ │ • Complex class inheritance
│ │ • autoname / before_naming hooks
│ │ • Unit-testable code
│ │
│ └─► NO: Use a Server Script when:
│ • You only have UI access (no bench CLI)
│ • Logic is simple validation / field calculation
│ • You need a quick API endpoint
│ • You need dynamic permission filtering
│
└─► Is logic > 50 lines or needs external libraries?
├─► YES → Document Controller in a custom app
└─► NO → Server Script is fine
doc # Current document (Document Event only)
frappe # Core namespace — ALWAYS available
frappe.db # Database operations
frappe.utils # Date, number, string utilities
frappe.session # Current session (user, csrf_token)
frappe.form_dict # Request parameters (API scripts)
frappe.response # Response object (API scripts)
frappe.request # Werkzeug request object
frappe.qb # Query Builder (v14+)
json # Python json module (pre-loaded)
# Documents
frappe.get_doc(doctype, name) # Fetch document
frappe.new_doc(doctype) # Create new document
frappe.get_cached_doc(doctype, name) # Cached fetch (read-only)
frappe.get_last_doc(doctype) # Most recent document
frappe.get_mapped_doc(...) # Map fields between DocTypes
frappe.delete_doc(doctype, name) # Delete document
frappe.rename_doc(doctype, old, new) # Rename document
# Querying
frappe.get_all(doctype, filters, fields, order_by, limit) # No permission check
frappe.get_list(doctype, filters, fields, order_by, limit) # With permission check
frappe.db.get_value(doctype, name, fieldname)
frappe.db.get_single_value(doctype, fieldname)
frappe.db.set_value(doctype, name, fieldname, value)
frappe.db.exists(doctype, name_or_filters)
frappe.db.count(doctype, filters)
frappe.db.sql(query, values, as_dict) # ALWAYS parameterize!
frappe.db.escape(value) # SQL escape
frappe.db.commit() # ONLY in Scheduler scripts
frappe.db.rollback() # ONLY in Scheduler scripts
# Messaging
frappe.throw(msg, exc, title) # Stop execution + show error
frappe.msgprint(msg, title, indicator) # User notification
frappe.log_error(message, title) # Error Log entry
# HTTP (yes, these work in sandbox!)
frappe.make_get_request(url, params, headers)
frappe.make_post_request(url, data, headers)
frappe.make_put_request(url, data, headers)
# Email
frappe.sendmail(recipients, sender, subject, message)
# Utilities
frappe.utils.today() # "2024-01-15"
frappe.utils.now() # "2024-01-15 10:30:00"
frappe.utils.now_datetime() # datetime object
frappe.utils.add_days(date, n) # Date arithmetic
frappe.utils.add_months(date, n)
frappe.utils.date_diff(d1, d2) # Days between dates
frappe.utils.flt(val) # Safe float (None → 0.0)
frappe.utils.cint(val) # Safe int (None → 0)
frappe.utils.cstr(val) # Safe string (None → "")
frappe.parse_json(string) # JSON string → dict/list
frappe.as_json(obj) # dict/list → JSON string
frappe.render_template(template, ctx) # Jinja rendering
frappe.get_url() # Site URL
frappe.get_hooks(hook) # Read app hooks
run_script(script_name, **kwargs) # Call another Server Script
# Session / Permissions
frappe.session.user # Current user email
frappe.get_roles(user) # User's roles list
frappe.has_permission(doctype, ptype, doc)
frappe.get_fullname(user) # User's display name
_("translatable string") # Translation function
str, int, float, bool, list, dict, tuple, set # Types
range, enumerate, zip, map, filter # Iteration
sum, min, max, len, sorted, reversed # Aggregation
isinstance, type, hasattr, getattr # Introspection
all, any, abs, round, divmod # Math/logic
print # → server log
True, False, None # Constants
open, file # No file I/O
eval, exec, compile # No dynamic code execution
__import__ # No imports (this is the root cause)
globals, locals # No scope introspection
# Config: Reference DocType = Sales Invoice, Event = Before Save
if doc.grand_total < 0:
frappe.throw("Total MUST NOT be negative")
doc.requires_approval = 1 if doc.grand_total > 10000 else 0
# Config: API Method = get_customer_orders, Allow Guest = No
# Endpoint: /api/method/get_customer_orders
customer = frappe.form_dict.get("customer")
if not customer:
frappe.throw("Parameter 'customer' is required")
orders = frappe.get_all("Sales Order",
filters={"customer": customer, "docstatus": 1},
fields=["name", "grand_total", "status"],
order_by="creation desc",
limit=20
)
frappe.response["message"] = {"orders": orders, "count": len(orders)}
# Config: Event Frequency = Cron, Cron Format = 0 9 * * *
overdue = frappe.get_all("Sales Invoice",
filters={"status": "Unpaid", "due_date": ["<", frappe.utils.today()], "docstatus": 1},
fields=["name", "customer", "grand_total"]
)
for inv in overdue:
frappe.log_error(f"Overdue: {inv.name} ({inv.customer})", "Invoice Reminder")
frappe.db.commit() # ALWAYS commit in Scheduler scripts
# Config: Reference DocType = Sales Invoice
# Variables available: user, conditions
roles = frappe.get_roles(user)
if "System Manager" in roles:
conditions = ""
elif "Sales User" in roles:
conditions = f"`tabSales Invoice`.owner = {frappe.db.escape(user)}"
else:
conditions = "1=0"
| Feature | v14 | v15 | v16 |
|---|---|---|---|
| Server Scripts enabled | By default | Disabled by default | Disabled by default |
| Enable command | Not needed | bench set-config -g server_script_enabled 1 | Same as v15 |
frappe.qb (Query Builder) | Available | Available | Available |
run_script() for libraries | v13+ | Available | Available |
frappe.make_get_request() | Available | Available | Available |
| Frappe Cloud shared bench | Supported | NOT supported | NOT supported |
import — everything is in the frappe namespacedoc.save() inside a Before Save script — causes infinite loopfrappe.db.commit() in Document Event scripts — framework handles itfrappe.db.commit() at the end of Scheduler scripts%(var)s with dict, NEVER f-strings in SQLWhat SKILL.md sets; muted values are Claude Code's defaults
90 skills in this plugin
/frappe-add-service-composeAdd a new microservice to dev-podman-compose.yml with proper configuration, dependencies, and networking./frappe-agent-debuggerUse when debugging Frappe errors, using bench console for live inspection, analyzing tracebacks, or reading Frappe log files. Prevents wasted debugging time from ignoring log context, misreading tracebacks, and not using bench console effectively. Covers bench console, frappe.logger, error log DocType, traceback analysis, common error patterns, log file locations, pdb/debugger integration, VS Code DAP, profiling, Frappe Recorder, mariadb diagnostics. Keywords: debug, bench console, traceback, error log, frappe.logger, pdb, debugging, log analysis, inspect, VS Code, DAP, profiling, recorder, mariadb, monitor, ERPNext error, how to debug, find the bug, what went wrong, stack trace, error message..
/frappe-agent-interpreterUse when receiving vague or unclear ERPNext/Frappe development requests that need interpretation. Transforms requirements like 'make invoice auto-calculate' or 'add approval workflow' into concrete technical specifications. Determines which Frappe mechanisms to use and maps to the full 61-skill catalog. Keywords: vague requirement, clarify scope, translate business need, technical spec, implementation plan, what does this mean, unclear requirement, translate to code, how to build this.
/frappe-agent-migratorUse when migrating a Frappe app between major versions, detecting breaking API changes, or resolving post-migration errors. Prevents failed migrations from undetected deprecated APIs, removed methods, and changed function signatures. Covers breaking change detection v14-v15-v16, deprecated API mapping, migration checklist, common migration errors, automatic fix suggestions. Keywords: migration, version upgrade, breaking changes, deprecated API, v14, v15, v16, migrate, compatibility, upgrade ERPNext, version change breaks, after update errors, deprecated method..
/frappe-api-handlerGenerate whitelisted API methods and REST endpoints for standard Frappe and microservices./frappe-app-installation-validatorValidate Frappe app installability and migration using SNE images in a containerized environment.npx claudepluginhub vyogotech/frappe-apps-manager --plugin frappe-apps-managerOther plugins' skills for the same job