Frappe Framework

Frappe Framework

Server Scripts

Server Scripts

Een Server Script draait Python op de server bij een documentgebeurtenis. Zelfde doel als Client Scripts maar met volledige DB-toegang en vertrouwde uitvoering — voor security-kritische validatie, side-effects en achtergrondwerk.

Aanmaken

Customize → Server Script → New:

Script Type Wanneer
DocType Event Bij een documentlifecycle (validate, before_save, on_submit, on_cancel, on_trash)
API Method Beschikbaar op /api/method/<uw.pad> voor externe callers
Scheduler Event Op een cron-schema (uurlijks, dagelijks, custom)
Permission Query Retourneert een SQL-fragment om lijstweergaven te filteren

DocType-event voorbeeld

# Triggert op validate van Verkoopfactuur
if doc.grand_total > 100000 and not doc.approved_by_manager:
    frappe.throw(_("Facturen boven 100k vereisen managergoedkeuring"))

if doc.customer:
    customer = frappe.get_doc("Customer", doc.customer)
    outstanding = customer.outstanding or 0
    if customer.credit_limit and (doc.grand_total + outstanding) > customer.credit_limit:
        frappe.throw(_("Klant boven kredietlimiet"))

doc is het huidige document; frappe is de frameworkmodule; _() vertaalt strings.

Scheduler-voorbeeld

# Dagelijks: notificeer boekhouding bij vervallen facturen
overdue = frappe.get_all("Sales Invoice", filters={
    "due_date": ["<", frappe.utils.nowdate()],
    "outstanding_amount": [">", 0],
    "docstatus": 1
})
if overdue:
    frappe.sendmail(
        recipients=["accounts@example.com"],
        subject=f"{len(overdue)} vervallen facturen",
        message="<br>".join(i.name for i in overdue)
    )

API-method voorbeeld

# Aanroepen op /api/method/<uw.pad> — args uit request payload
return {"ok": True, "result": frappe.utils.flt(amount) * 1.21}

Sandbox

Server Scripts draaien in een Python-sandbox: geen import van willekeurige modules, geen schrijven naar bestandssysteem, geen subprocess. Voor onbeperkt Python of externe dependencies: maak een Frappe-app — zie Ontwikkelaars.

Last updated 6 months ago
Was this helpful?
Thanks!