Server Script
A Server Script lets you dynamically define a Python Script that is executed on the server on a document event or API
Last updated
A Server Script lets you dynamically define a Python Script that is executed on the server on a document event or API
Last updated
Introduced in Version 12
To create a Server Script
If your site is being hosted on , contact support to activate Server Script. In case of self-hosted accounts, set server_script_enabled
as true in site_config.json of your site.
To add/edit Server Script, ensure your role is System Manager.
Create a new server script via "New Server Script" in the toolbar.
Select the type of server script: Document Event, API, Permission Query.
Set the document type and event name, or method name, script and save.
Server script must be enabled via site_config.json
For scripts that are to be called via document events, you must set the Reference Document Type and Event Name to define the trigger
Before Insert
Before Save
After Save
Before Submit
After Submit
Before Cancel
After Cancel
Before Delete
After Delete
Before Save (Submitted Document)
After Save (Submitted Document)
You can create a new API that can be accessed via api/method/[methodname]
by the script type "API"
If you want the guest user to access the API, you must check on "Allow Guest"
The response can be set via frappe.response["message"]
object
This type of script allows you to add custom conditions in where clause for a DocType list query.
For example, let's say you want to show the list of ToDo records to a user only if they assigned the record or it was assigned to them. This can implemented by the following script:
The resulting select
query will look something like this:
Now, the list view of ToDo will show restricted records. This will also restrict the results shown in Link fields.
Frappe Framework uses the RestrictedPython library to restrict access to methods available for server scripts. Only the safe methods, listed below are available in server scripts
Script Type: Before Save
Script Type: "Before Save"
Script Type: "After Save"
Script Type: API
Method Name: test_method
Request: /api/method/test_method
Was this article helpful?Give Feedback.