What is a Server Script in Frappe?
A Server Script in Frappe v15 is a backend scripting feature that allows you to write Python code directly within the system to customize business logic without modifying core application files.
It enables:
- Automation of workflows
- Validation and data processing
- Custom API creation
- Event-based backend logic
Server Scripts run securely on the server using Frappe’s execution environment.
Why Use Server Script in ERPNext?
Server Scripts are used to implement backend logic dynamically and safely.
Key Benefits:
- No need to create custom apps
- Upgrade-safe customization
- Centralized business logic
- Supports automation and APIs
How to Create a Server Script in Frappe v15?
Step-by-Step:
- Go to Desk → Server Script
- Click New
- Select the Script Type
- Choose the Reference DocType (if applicable)
- Write your Python script
- Save and enable the script
What Are the Types of Server Scripts?
Frappe v15 supports multiple types of Server Scripts.
Script Types Overview:
| Script Type | Description |
| DocType Event | Triggered on document lifecycle events |
| API | Create custom API endpoints |
| Scheduler Event | Run scripts periodically |
How Do DocType Event Scripts Work?
DocType Event scripts run automatically during document actions.
Supported Events:
- before_insert
- after_insert
- before_save
- after_save
- before_submit
- after_submit
- before_cancel
- after_cancel
Example: Validate Before Save
if not doc.customer:
frappe.throw(“Customer is required”)
Example: Auto Update Field
doc.status = “Validated”
How to Create Custom API Using Server Script?
You can expose backend logic as an API endpoint.
Example: Simple API Script
frappe.response[‘message’] = “Hello from Server Script”
This API can be accessed via HTTP request.
How to Use Scheduler Event Scripts?
Scheduler scripts run automatically at defined intervals.
Example:
frappe.logger().info(“Scheduled task executed”)
Useful for background jobs like:
- Data cleanup
- Notifications
- Periodic updates
Common Server Script Examples
1. Auto Set Default Value
if not doc.status:
doc.status = “Open”
2. Prevent Duplicate Records
existing = frappe.db.exists(‘Customer’, {’email_id’: doc.email_id})
if existing:
frappe.throw(“Customer already exists”)
3. Call Another DocType
customer = frappe.get_doc(‘Customer’, doc.customer)
doc.customer_group = customer.customer_group
Security Model of Server Script in v15
Frappe v15 uses a restricted execution environment for Server Scripts.
Key Security Features:
- Limited Python execution (safe environment)
- Restricted imports
- Controlled database access
- Role-based permissions
Prevents misuse and protects system integrity.
Best Practices for Server Script
- Keep scripts short and focused
- Avoid heavy loops or long-running tasks
- Use proper error handling (frappe.throw)
- Test scripts in UAT before production
- Use Scheduler for background jobs
Troubleshooting Common Issues
Issue: Script Not Triggering
- Verify selected event type
- Ensure script is enabled
Issue: Permission Errors
- Check user roles and permissions
Issue: API Not Working
- Confirm correct endpoint and method
- Check server logs
Integration with Other Frappe Components
Server Scripts work seamlessly with:
- Client Scripts → UI logic
- System Console → Testing scripts
- REST API → External integrations
- Workflows → Business processes
Real-World Use Cases
Manufacturing Industry
- Auto-update production status
Real Estate
- Validate booking rules
Finance
- Prevent incorrect transactions
Target Audience
- ERPNext Developers
- Backend Developers
- ERP Consultants
- System Administrators
Technical Prerequisites
- Knowledge of Python
- Understanding of Frappe ORM
- Familiarity with DocTypes and workflows
Industry Relevance
Server Scripts are critical for:
- Manufacturing ERP automation
- CRM workflows
- Financial validations
- Inventory control
Official References
Frappe Server Script Docs:
https://docs.frappe.io/framework/user/en/desk/scripting/server-script
Frappe GitHub v15:
https://github.com/frappe/frappe/tree/version-15
FAQs (AEO Optimized)
What is the difference between Client Script and Server Script in Frappe?
Client Script runs in the browser using JavaScript, while Server Script runs on the server using Python for backend logic.
Can Server Script replace custom apps?
For simple logic, yes. But complex features still require custom apps.
Is Server Script safe in Frappe v15?
Yes. It runs in a restricted and secure execution environment.
Conclusion
The Frappe Server Script in Version 15 is a powerful backend customization tool that enables automation, validation, and API creation without modifying core code. When used effectively, it significantly enhances ERPNext flexibility and development speed.