Workflows
Workflows run in the browser today. This page shows how they're built and how to run them on your server, on events and on a schedule, so they work when nobody has the app open.
What the demo does today
WorkflowsProvider (src/components/providers/workflows-provider.tsx) holds the workflows and runs them in the browser: when someone presses Run now, when a contract is signed, and when a lead is created or changes stage. Scheduled workflows show a schedule and past runs, but nothing runs them on a timer. Slack and webhook steps are counted as done without sending anything.
The definitions are in src/lib/data/workflows.ts (Workflow, TRIGGERS, ACTIONS and the sample WORKFLOWS). The rules are in src/lib/workflow-engine.ts.
How a run works
The engine is made of pure functions with no React in them, so the same file runs on your server:
| Function | What it does |
|---|---|
matchSubjects(workflow, ctx) | The records the trigger matches right now: contracts, invoices, leads or people |
passes(subject, conditions) | Checks the If conditions (value, plan, auto-renew, days late, amount, stage, source) |
plan(actions, subject, ctx) | Turns each step into an effect: a task, email, note, owner change, Slack message, notification or webhook |
fill(text, subject, ctx) | Fills placeholders such as {customer}, {lead} and {amount} |
The provider then applies each effect and skips anything that already happened: a task with the same title on the same record, an email thread with the same subject, a note with the same text, a notification for the same record. That's what makes running a workflow twice safe; keep the same checks on the server.
Triggers and when to run them
| Trigger | Kind | Run it |
|---|---|---|
| Contract is signed | Event | When a contract moves from sent to active |
| Lead is created | Event | When a lead is saved for the first time |
| Lead changes stage | Event | When an open lead moves stage (optionally to one stage) |
| Contract enters renewal window | Scheduled | Daily: contracts ending within N days |
| Invoice becomes overdue | Scheduled | Daily: invoices past their due date |
| Lead goes quiet | Scheduled | Daily: open leads with no activity for N days |
| Relationship goes cold | Scheduled | Daily: people going cold for N days (see Network) |
| Every week | Scheduled | Weekly, with a summary of the week |
The time-based triggers only fire when something checks them, so they need a scheduled job on the server. Each workflow's schedule holds a label and a cron expression you can use directly.
Run workflows on the server
Save workflows and their runs in your database, and load them into the provider instead of WORKFLOWS.
Write one server function that loads the records a trigger needs, calls matchSubjects, passes and plan, applies the effects with the same already-done checks, and saves a run.
Call it from your events: after a contract is signed, a lead is created or a lead moves stage, pass that record in as the subject.
Run the scheduled triggers from a cron job, such as Vercel Cron, that calls a protected Route Handler once a day.
Remove the browser's event effects in WorkflowsProvider once the server runs them, so nothing runs twice. Keep Run now: it can call the same server function.
Cron runs in UTC. A workflow that should run at 9am for a team in another time zone needs its own schedule, or a check of the workspace's time zone inside the job.
Steps that call other services
- Send email starts a Messages thread in the demo. Send it with your email provider instead (see Email).
- Slack posts to the channel in the step with Slack's API and a bot token your workspace connects.
- Webhook sends a POST with the subject as JSON. Sign the body with a secret, time out after a few seconds, and retry a failed call a few times before marking it failed.
- Assign owner (round robin) picks from the comma-separated names in the step, the same for a lead every time (
roundRobin). Use teammate ids on the server.
A step whose app isn't connected is counted as blocked, and the run says so. Check the connection on the server too.
Run history
Each workflow keeps its last 20 runs (when, what started it, how many records matched, and what each step did). Save runs as rows linked to the workflow, so the Recent runs list and the dashboard's Workflow runs card read real history.