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 typeWhat it runsWho can create it
App-defined actionA Python method that an app declares in its hooks.pyApp developers
Server ScriptA server script of type Workflow TaskSystem managers
WebhookA webhook with the Doc Event workflow_transitionSystem 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 ​

  1. Open the Workflow.
  2. In the Transitions table, open the row of the transition.
  3. In Transition Tasks, create a new Workflow Transition Tasks document.
  4. 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.
  5. Save the tasks, and then save the workflow.

Create a server script task ​

  1. Open the Server Script list, and select New.
  2. In Script Type, select Workflow Task.
  3. Write the script. It gets doc, the document that moves between states.
  4. 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 ​

  1. Open the Webhook list, and select New.
  2. In Doc Event, select workflow_transition.
  3. Fill in the request details, and save.
  4. 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.