cardinal

Automation steps

Add step types to the automation builder with Cardinal.Automation.Step.

Users build automations in the desk from a palette of steps. An app adds its own step types. A site gets the steps of the apps it has installed.

Register steps

crm/app.exs
automation_steps: [
  Crm.Steps.AdjustLeadScore,
  Crm.Steps.SetLeadTemperature,
  Crm.Steps.ConvertLead
]

cardinal app check reports a module without type/0 and two modules with the same type.

A step

CRM's Adjust lead score, shortened:

crm/lib/steps/adjust_lead_score.ex
defmodule Crm.Steps.AdjustLeadScore do
  @behaviour Cardinal.Automation.Step
  alias Cardinal.DB

  @impl true
  def type, do: "crm_adjust_lead_score"

  @impl true
  def schema do
    %{
      "label" => "Adjust lead score",
      "description" => "Adds points to a CRM Lead's Lead Score.",
      "inputs" => %{
        "amount" => %{"type" => "number", "required" => true, "label" => "Points to add"},
        "reason" => %{"type" => "string", "label" => "Reason"}
      },
      "outputs" => %{"old_score" => "number", "new_score" => "number"}
    }
  end

  @impl true
  def rights(_inputs, _trigger), do: [{"CRM Lead", ["write"]}]

  @impl true
  def run(inputs, run, ctx) do
    with {:ok, lead} <- DB.get_doc(ctx, "CRM Lead", run["name"], for_update: true),
         old = lead["lead_score"] || 0,
         new = old + inputs["amount"],
         {:ok, _rec, fx} <- DB.set_value(ctx, "CRM Lead", run["name"], %{"lead_score" => new}, skip_hooks: true) do
      {:ok, "next", %{"old_score" => old, "new_score" => new}, fx}
    end
  end
end

Callbacks

CallbackReturns
type/0The step's name: letters, digits and _, unique across installed apps. Prefix it with the app's name
schema/0Its inputs, outputs, branches and wait, plus palette keys such as label and description
rights(inputs, rectype)[{rectype, ptypes}] the step needs. Saving an enabled automation checks them for its owner
run(inputs, run, ctx){:ok, branch, outputs, effects} or {:error, reason}
wait(inputs, run, ctx)Optional. For a step with "wait" => true: {:ok, deadline} or {:ok, deadline, event}
preview(inputs, run, ctx)Optional. What the step would do in a dry run, for a step with "effects" => "external"

Inputs

An input's type is boolean, number, string, date, datetime, time, table, field (a field of the trigger's RecType) or any. type_of makes an input take the type of a field input. literal: true refuses an expression, for text a later step treats as the author's own, such as an AI prompt. branches defaults to ["next"].

How a step runs

  • run/3 runs as the automation's owner, inside its job's transaction.
  • run holds rectype, name, doc, steps (earlier nodes' outputs by node id) and expr_inputs (inputs an expression filled). Never run those inputs as a template or code.
  • {:error, reason} takes back the step's writes and fails the run. A raise rolls the job back and Oban retries it, 5 attempts in all.
  • A step whose effect a rollback cannot undo, such as a mail or an HTTP call, enqueues a job for it in the transaction.

Waiting on another system

A step that calls an outside service does not call it in run/3, because the job holds the run's row lock. Instead:

Set "wait" => true in the schema. wait/3 enqueues the call's job with a fresh event name and returns {:ok, deadline, event}.

The job calls the service with no transaction open.

The job ends the wait with Cardinal.Automation.resume(ctx, event, branch, outputs), or Cardinal.Automation.fail(ctx, event, message). If the deadline passes first, the run takes the timeout branch.

Build automations from code

Cardinal.Automation.catalog(ctx) returns the step catalog of the installed apps. Cardinal.Automation.check(ctx, rec) checks an Automation as an enabled save would, without saving. warnings/2 also returns the warnings. Eden's Adam uses these to propose automations.

On this page