Run tasks on workflow transitions
A transition task runs when a document moves between two workflow states. For example, when a document is approved, a task can create a related document or send the data to another system.
This page is for system managers and developers.
INFO
Workflow transition tasks are new in version 16.
Types of task
| Task type | What it runs | Who can create it |
|---|---|---|
| App-defined action | A Python method that an app declares in its hooks.py | App developers |
| Server Script | A server script of type Workflow Task | System managers |
| Webhook | A webhook with the Doc Event workflow_transition | System managers |
Run modes
- Synchronous (the default): the tasks run one by one when the transition starts. If one task fails, ERPNext reverses the transition.
- Asynchronous: select Asynchronous on the task. The task runs in a separate background job after the transition is complete. It cannot stop the transition.
Add tasks to a transition
- Open the Workflow.
- In the Transitions table, open the row of the transition.
- In Transition Tasks, create a new Workflow Transition Tasks document.
- In its Tasks table, add one row for each task:
- Task: the task type or the app-defined action.
- Link: the server script or the webhook, for those types.
- Enabled: keep it selected.
- Asynchronous: select it to run the task in the background.
- Save the tasks, and then save the workflow.
Create a server script task
- Open the Server Script list, and select New.
- In Script Type, select Workflow Task.
- Write the script. It gets
doc, the document that moves between states. - Save the script, and link it in a transition task.
To learn more, read the Frappe Framework documentation on server scripts.
Create a webhook task
- Open the Webhook list, and select New.
- In Doc Event, select
workflow_transition. - Fill in the request details, and save.
- Link the webhook in a transition task.
To learn more, read the Frappe Framework documentation on webhooks.
Declare an app-defined action
An app declares its actions with the workflow_methods hook. Each method takes the document as its only argument.
python
# myapp/hooks.py
workflow_methods = [
{"name": "Create a customer", "method": "myapp.shop.utils.create_customer"},
]python
# myapp/shop/utils.py
import frappe
def create_customer(doc):
customer = frappe.new_doc("Customer")
customer.customer_name = "Customer " + doc.name
customer.customer_type = "Individual"
customer.save()The action then appears in the Task dropdown. Users cannot create app-defined actions from the UI. Use a server script instead.