Workflow Automation
Relevant source files
The following files were used as context for generating this wiki page:- packages/twenty-server/src/modules/workflow/workflow-tools/tools/create-complete-workflow.tool.ts
- packages/twenty-shared/src/workflow/schemas/workflow-trigger-schema.ts
- packages/twenty-shared/src/workflow/schemas/workflow-action-schema.ts
- packages/twenty-server/src/engine/workspace-manager/workspace-sync-metadata/standard-agents/agents/workflow-builder-agent.ts
- packages/twenty-front/src/modules/workflow/types/Workflow.ts
- packages/twenty-server/src/modules/workflow/common/workspace-services/workflow-common.workspace-service.ts
- packages/twenty-server/src/engine/workspace-manager/standard-objects-prefill-data/prefill-workflows.ts
- packages/twenty-front/src/modules/workflow/workflow-diagram/components/WorkflowVisualizer.tsx
- packages/twenty-server/src/modules/workflow/workflow-runner/workflow-run-queue/cron/jobs/workflow-clean-workflow-runs.cron.job.ts
The Workflow Automation system in Twenty allows users to create and manage automated processes that respond to events within the CRM. These workflows are constructed from triggers and a series of action steps, which can perform operations like creating records, sending emails, or executing custom code. The system is designed to be flexible, with a visual editor for building workflows and an AI-powered "Workflow Builder" agent to assist in their creation.
Workflows are versioned, allowing for drafts and active versions to coexist. The execution of a workflow is managed through a queue system, and historical runs are stored for auditing and troubleshooting purposes.
Core Concepts & Data Model
The workflow system is built around a few key entities that manage the structure, versioning, and execution of automations.
The diagram below shows the relationship between the main workflow entities.
Workflow*: The main container for an automation. It holds general information like the name and status (ACTIVE, DRAFT, DEACTIVATED).
- WorkflowVersion: A specific implementation of a workflow, containing the trigger and steps. A workflow can have multiple versions, but only one can be active at a time. Statuses include
DRAFT,ACTIVE,DEACTIVATED, andARCHIVED. - WorkflowRun: An instance of an executed workflow version. It stores the status (
COMPLETED,FAILED, etc.) and the state of the execution flow.
Sources: packages/twenty-front/src/modules/workflow/types/Workflow.ts:80-112, packages/twenty-server/src/modules/workflow/common/standard-objects/workflow.workspace-entity.ts, packages/twenty-server/src/modules/workflow/common/standard-objects/workflow-version.workspace-entity.ts, packages/twenty-server/src/modules/workflow/common/standard-objects/workflow-run.workspace-entity.ts
Workflow Structure
A workflow consists of a single Trigger and one or more Action Steps. The trigger initiates the workflow, and the data flows sequentially through the connected steps.
This diagram illustrates a simple workflow structure.
Triggers
Triggers are the entry point of a workflow. They define the event that will cause the workflow to execute.
| Trigger Type | Description |
|---|---|
DATABASE_EVENT | Executes when a record is created, updated, or deleted. Replaces the deprecated RECORD_CREATED, RECORD_UPDATED, RECORD_DELETED types. |
MANUAL | Executes when a user manually launches the workflow from the UI. |
CRON | Executes on a schedule, defined by a cron expression or a simplified schedule. |
WEBHOOK | Executes when an HTTP request is sent to a specific endpoint. |
Sources: packages/twenty-shared/src/workflow/schemas/workflow-trigger-schema.ts:10-14, packages/twenty-server/src/engine/workspace-manager/workspace-sync-metadata/standard-agents/agents/workflow-builder-agent.ts:13
CRON Trigger Settings
CRON triggers have a specific settings schema that must be followed. The type property determines the schedule format.
| Type | Required Fields | Description |
|---|---|---|
DAYS | schedule: { day: number, hour: number, minute: number } | Runs on a daily schedule. |
HOURS | schedule: { hour: number, minute: number } | Runs every X hours at Y minutes past the hour. |
MINUTES | schedule: { minute: number } | Runs every X minutes. |
CUSTOM | pattern: string | Runs based on a standard cron expression. |
Actions (Steps)
Actions are the individual operations performed within a workflow. Each action is a step in the process and can use outputs from previous steps.
| Action Type | Description |
|---|---|
CREATE_RECORD | Creates a new record for a specified object. |
UPDATE_RECORD | Updates an existing record. |
DELETE_RECORD | Deletes an existing record. |
UPSERT_RECORD | Creates a new record or updates an existing one if it's found. |
FIND_RECORDS | Finds records based on specified criteria. |
SEND_EMAIL | Sends an email. |
CODE | Executes a serverless function. |
FORM | Presents a form to the user to gather input. |
HTTP_REQUEST | Makes an HTTP request to an external service. |
AI_AGENT | Executes an AI agent. |
ITERATOR | Iterates over a list of items. |
DELAY | Pauses the workflow for a specified duration. |
FILTER | Filters data based on conditions. |
EMPTY | A placeholder action. |
Sources: packages/twenty-shared/src/workflow/schemas/workflow-action-schema.ts:245-260, packages/twenty-front/src/modules/workflow/types/Workflow.ts:51-66
Workflow Creation
Workflows can be created manually through the UI or programmatically, often with the help of an AI agent. The create_complete_workflow tool is a key component for programmatic creation.
create_complete_workflow Tool
This server-side tool allows for the creation of a full workflow—including its trigger, steps, UI positions, and connections—in a single operation. It is heavily utilized by the workflow-builder AI agent.
The sequence diagram below outlines the execution flow of the create_complete_workflow tool.
The tool first creates the Workflow entity, followed by the WorkflowVersion with its trigger and steps. It then optionally sets the visual layout positions and edges for the UI, and can activate the new version immediately.
AI-Powered Workflow Building
The workflow-builder is a specialized AI agent designed to help users create and manage workflows. It understands the core concepts of triggers, steps, and data flow.
Capabilities:
- Create workflows from natural language descriptions.
- Modify existing workflows (add, remove, or update steps).
- Explain the structure of a workflow and suggest improvements.
The agent is prompted to use the create_complete_workflow tool as the most efficient method for creating new workflows. It is provided with schema definitions and examples to ensure it generates valid workflow structures.
Sources: packages/twenty-server/src/engine/workspace-manager/workspace-sync-metadata/standard-agents/agents/workflow-builder-agent.ts:7-55, packages/twenty-server/src/engine/metadata-modules/ai/ai-agent/constants/agent-system-prompts.const.ts:28-31
Frontend Visualization
The twenty-front package contains the WorkflowVisualizer.tsx component, which provides a graphical interface for building and viewing workflows. It uses react-flow to render the nodes (trigger and steps) and the edges that connect them.
Key features of the visualizer include:
- Adding new triggers and steps.
- Displaying the name and icon for each step.
- Editing step details in a command menu side panel.
- A "Tidy up" feature, implemented in the
useTidyUpWorkflowVersionhook, which automatically organizes the layout of the diagram nodes for better readability.
Sources: packages/twenty-front/src/modules/workflow/workflow-diagram/components/WorkflowVisualizer.tsx, packages/twenty-front/src/modules/workflow/workflow-version/hooks/useTidyUpWorkflowVersion.ts:13-81
Lifecycle and Management
Statuses and Activation
A Workflow can have one of the following statuses: DRAFT, ACTIVE, or DEACTIVATED. A WorkflowVersion can be DRAFT, ACTIVE, DEACTIVATED, or ARCHIVED.
When a workflow version is activated, its status is set to ACTIVE, and the parent workflow's lastPublishedVersionId is updated. The parent workflow's status is also updated to ACTIVE. Deleting a workflow will deactivate any active versions and change the workflow's status to DEACTIVATED.
Sources: packages/twenty-front/src/modules/workflow/types/Workflow.ts:80-85, packages/twenty-server/src/modules/workflow/common/workspace-services/workflow-common.workspace-service.ts:251-285
Prefilled Workflows
The system includes pre-filled workflows to provide users with out-of-the-box examples. The "Quick Lead" workflow is one such example, which is created when a new workspace is provisioned. It uses a MANUAL trigger and a FORM step to capture lead details, followed by CREATE_RECORD steps to create a Company and a Person.
Workflow Run Cleanup
To manage database size and performance, a cron job runs periodically to clean up old workflow execution history. The WorkflowCleanWorkflowRunsJob is scheduled to run daily (0 0 * * *).
The job performs the following actions for each active workspace:
- Identifies completed or failed workflow runs that are older than 14 days.
- For each workflow, it also identifies runs beyond the most recent 1000.
- Deletes the identified old runs from the
workflowRuntable.
Summary
The Workflow Automation feature is a powerful and extensible system within Twenty. It combines a robust backend architecture with a user-friendly visual editor and AI-assisted creation. By leveraging triggers, a variety of actions, and a versioning system, it enables complex business processes to be automated efficiently. The lifecycle of workflows is managed from creation and activation to eventual cleanup of run histories, ensuring the system remains performant and maintainable.