Skip to main content

Workflow State Transition Automation

Description

This complex automation demonstrates handling workflow state transitions with conditional logic. It triggers after any workflow movement and executes different actions based on the specific transition that occurred. The automation shows advanced patterns including queue management, entity queries, conditional action branching based on workflow actions, and integration with multiple business processes. This is essential for implementing state-machine-driven business logic.

Use Case

Used in a subscription management system to orchestrate complex workflow transitions. When a subscription moves through states (validation, customer matching, welcome pack, active, deferred), different business logic executes. For example, moving to "pending_customer_match" triggers customer matching logic, while moving to "active" calculates the next debit run date.

Key Features

  • Triggers on any workflow state change
  • Conditional action execution based on transition type
  • Message queue integration for async processing
  • Entity querying and data retrieval
  • Complex condition evaluation with OR/AND combinators
  • Access to old and new entity states
  • Multiple action paths based on workflow transitions

JSON Definition

{
"key": "after_wf_move_subscription",
"name": "After WF move Subscription",
"entityKey": "subscription",
"app": "crm",
"triggerType": "afterWorkflowMove",
"triggerParams": {},
"actions": [
{
"name": "add to queue subscription",
"key": "add_to_queue_subscription",
"actionTypeKey": "add_message_queue",
"params": {
"queueName": "index_subscription",
"message": "{{{subscriptionId: trigger.entityId}}}",
"priority": "long"
}
},
{
"name": "update customer flags",
"key": "update_customer_flags",
"actionTypeKey": "update_customer_flags_from_subscription",
"params": {
"subscriptionId": "{{trigger.entityId}}"
}
},
{
"name": "get subscription",
"key": "get_subscription",
"actionTypeKey": "entity_find",
"params": {
"entityKey": "subscription",
"query": "{{{$select: ['id', 'customer_id', '_workflow_outcome'], $where: {id: trigger.entityId}}}}"
}
}
],
"conditionalActions": [
{
"condition": {
"rules": [
{
"field": "{{trigger.actionKey}}",
"value": "reprocess_validation",
"operator": "equals"
}
],
"combinator": "or"
},
"actions": [
{
"name": "Execute validation step",
"key": "handle_step",
"actionTypeKey": "handle_subscription_validation_wf_step",
"params": {
"subscriptionId": "{{trigger.entityId}}"
}
}
]
},
{
"condition": {
"rules": [
{
"field": "{{trigger.actionKey}}",
"value": "pending_customer_match",
"operator": "equals"
},
{
"field": "{{trigger.actionKey}}",
"value": "reprocess_matching",
"operator": "equals"
}
],
"combinator": "or"
},
"actions": [
{
"name": "Execute customer match step",
"key": "handle_step",
"actionTypeKey": "handle_subscription_customer_match_wf_step",
"params": {
"subscriptionId": "{{trigger.entityId}}"
}
}
]
},
{
"condition": {
"rules": [
{
"field": "{{trigger.actionKey}}",
"value": "deferral_ended",
"operator": "equals"
},
{
"field": "{{trigger.actionKey}}",
"value": "reactivate",
"operator": "equals"
},
{
"field": "{{trigger.actionKey}}",
"value": "set_active",
"operator": "equals"
},
{
"field": "{{trigger.actionKey}}",
"value": "sent",
"operator": "equals"
}
],
"combinator": "or"
},
"actions": [
{
"name": "Update next_debit_run_dt",
"key": "handle_step",
"actionTypeKey": "handle_subscription_active_wf_step",
"params": {
"subscriptionId": "{{trigger.entityId}}"
}
}
]
}
]
}

Notes

  • trigger.actionKey contains the workflow action that caused the transition
  • trigger.oldEntity provides access to the entity state before transition
  • Conditional actions only execute when their conditions are met
  • The combinator determines how multiple rules are evaluated (AND/OR)
  • Queue priorities (realtime, normal, long) control processing order
  • Entity queries use MongoDB-like syntax for filtering
  • Actions can access outputs from previous actions via runbook.outputs