Server Script

A Server Script lets you dynamically define a Python Script that is executed on the server on a document event or API

Introduced in Version 12

1. How to create a Server Script arrow-up-right

To create a Server Script

  1. If your site is being hosted on howinibs.com, 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.

  2. To add/edit Server Script, ensure your role is System Manager.

  3. Create a new server script via "New Server Script" in the toolbar.

  4. Select the type of server script: Document Event, API, Permission Query.

  5. Set the document type and event name, or method name, script and save.

2. Features arrow-up-right

2.1 Enabling Server Script arrow-up-right

Server script must be enabled via site_config.json

bench --site site1.local set-config server_script_enabled true

2.2 Document Events arrow-up-right

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)

2.3 API Scripts arrow-up-right

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

2.4 Permission Query arrow-up-right

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.

2.5 Security arrow-up-right

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

3. Examples arrow-up-right

3.1 Change the value of a property before change arrow-up-right

Script Type: Before Save

3.2 Custom validation arrow-up-right

Script Type: "Before Save"

3.3. Auto Create To Do arrow-up-right

Script Type: "After Save"

  • Script Type: API

  • Method Name: test_method

Request: /api/method/test_method

Was this article helpful?Give Feedback.


Last updated