Server Script
Server Script
When a document event or API occurs, a server script allows you to dynamically define a Python script that is run on the server.
Introduced in Version 12
1. How to create a Server Script
Creating a Server Script
- Get in touch with support to activate Server Script if erpnext.com is hosting your website. Set server script enabled to true in your site's site config.json if you are using a self-hosted account.
- Make sure your role is System Manager before adding or editing Server Script.
- Choose "New Server Script" in the toolbar to create a new server script.
- Choose between Document Event, API, and Permission Query as the type of server script.
- Choose the document type, the event name (or method name), the script, and save.
2. Features
2.1 Enabling Server Script
Site config.json must be used to enable server script.
2.2 Document Events
You must set the Reference Document Type and Event Name to determine the trigger for scripts that will be called via document events.
- 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
You can establish a new API that the "API" script type can access by going to api/method/[methodname].
You must select "Allow Guest" if you want the visitor user to have access to the API.
With frappe, the response can be modified.
response["message"] object
2.4 Permission Query
With this kind of script, you can add unique conditions to a DocType list query's where clause.
Let's imagine, for instance, that you want a user to only see the list of ToDo records if they assigned the item or if it was assigned to them. The script below can be used to implement this:
Now, restricted records will appear in ToDo's list view. Moreover, this will limit the outcomes displayed in Link fields.
2.5 Security
For the purpose of limiting access to methods available to server scripts, Frappe Framework makes use of the RestrictedPython library. Just the following secure techniques are offered in server scripts.
3. Examples
3.1 Change the value of a property before change
3.2 Custom validation
Type of Script: "Before Save"
3.3. Auto Create To Do
Type of Script: "After Save"
3.4 API
- Script Type: API
- Method Name: test_method