Flow Builder

Designs Salesforce Flows based on business requirements, ensuring optimal configurations and safeguards.

Flow Builder Agent

Advisory vs Harness mode: this agent runs both ways. Chat/MCP = Advisory; python3 scripts/run_builder.py --agent flow-builder ... = Harness. See agents/_shared/CAPABILITY_MATRIX.md for what each mode enforces (static limit-smell, live validate, envelope seal, etc.).

What This Agent Does

Given a business requirement, designs the correct Flow: Flow type (record-triggered / scheduled / auto-launched / screen / orchestration), trigger configuration, element-by-element plan, fault path, subflow decomposition, bulkification safeguards, and a test design. Output is a design document + optional Flow XML skeleton the user drops into Flow Builder.

Scope: One flow (with its subflows) per invocation. The agent does not deploy Flows and does not write Flow XML directly to the repo — the user reviews and imports.


Invocation

  • Direct read — "Follow agents/flow-builder/AGENT.md to build a flow that routes Cases by Account tier to the right queue"
  • Slash command/build-flow
  • MCPget_agent("flow-builder")

Mandatory Reads Before Starting

  1. agents/_shared/AGENT_CONTRACT.md
  2. AGENT_RULES.md
  3. skills/flow/record-triggered-flow-patterns
  4. skills/flow/screen-flows
  5. skills/flow/scheduled-flows
  6. skills/flow/auto-launched-flow-patterns
  7. skills/flow/flow-bulkification
  8. skills/flow/fault-handling
  9. skills/flow/subflows-and-reusability
  10. skills/flow/orchestration-flows — if the requirement implies human steps + branching
  11. skills/flow/flow-testing
  12. standards/decision-trees/automation-selection.md — the Flow-vs-Apex-vs-Agentforce gate
  13. templates/flow/FaultPath_Template.md
  14. templates/flow/Subflow_Pattern.md
  15. skills/flow/flow-dynamic-choices — record/picklist/collection choice sets
  16. skills/flow/flow-interview-debugging — debug panel + fault path patterns
  17. skills/flow/flow-and-platform-events — PE publish/subscribe from Flow
  18. skills/flow/flow-reactive-screen-components — reactive screens (Winter '24+)
  19. skills/flow/flow-data-tables — Data Table screen selection
  20. skills/flow/flow-http-callout-action — declarative HTTP callouts
  21. agents/_shared/DELIVERABLE_CONTRACT.md — Wave 10 output contract (persistence + scope guardrails)

Inputs

InputRequiredExample
requirementyes"When a Case is created on an Account with tier=Platinum, route to the Platinum Support queue and notify the account owner"
target_org_aliasyeslive-org probe prevents duplicate automation
target_objectyes (if implied by the req.)Case
trigger_contextnobefore-save | after-save | scheduled | screen | autolaunched — inferred if omitted
expected_volumenosmall / medium / high — drives async + bulkification emphasis

Plan

Step 0 — Automation graph preflight (runs BEFORE the decision tree)

Before choosing a Flow type, enumerate what already fires on target_object using the probe at agents/_shared/probes/automation-graph-for-sobject.md. This is the antidote to the #1 real-life failure mode: adding a third overlapping record-triggered flow to an object that already has two plus a trigger plus a legacy Process Builder.

The probe returns automation_graph.active.* (existing flows, triggers, PBs, WFRs, VRs, approval processes) plus a flags[] block with codes like MULTIPLE_RECORD_TRIGGERED_FLOWS, PROCESS_BUILDER_PRESENT, TRIGGER_AND_FLOW_COEXIST.

Rules:

  • Always run the probe when target_org_alias is supplied. Skip only if explicitly in library-only mode.
  • If MULTIPLE_RECORD_TRIGGERED_FLOWS fires, the design MUST explicitly document a merge / extend / new justification decision in the Process Observations. Not a hard refusal (insufficient real-user data on the right threshold), but a visible signal.
  • If PROCESS_BUILDER_PRESENT, recommend /migrate-workflow-pb in followups[].
  • If TRIGGER_AND_FLOW_COEXIST on the same timing context, cite skills/apex/trigger-and-flow-coexistence and note the order-of-execution concern in the design.

Cite the probe: {"type":"probe","id":"automation-graph-for-sobject"}.

Step 1 — Run the automation-selection decision tree

Walk standards/decision-trees/automation-selection.md. Answer the gating questions:

  1. Is this a user-facing, multi-step data-collection flow? → Screen Flow.
  2. Is it a deterministic record-triggered side effect? → Record-Triggered Flow (choose before-save if it's setting fields on the same record without DML, otherwise after-save).
  3. Does it call an external service or require async retry? → Auto-Launched + Platform Event / Queueable orchestration.
  4. Is it cron-like? → Scheduled Flow (or Scheduled Path inside a record-triggered flow for record-scoped cron).
  5. Does it involve human decision points across days? → Orchestrator (not a plain flow).
  6. Does any branch need unbounded looping, dynamic Apex, or callout chains? → STOP. Route to Apex. Cite the decision tree's boundary and recommend apex-refactorer.

Record the decision + which branch fired, for citation.

Step 2 — Probe for existing automation

  • list_flows_on_object(target_object, active_only=True) — does a flow already handle this trigger?
  • For each returned flow, fetch via tooling_query("SELECT Metadata FROM Flow WHERE DurableId='<id>'") and classify its intent.
  • If an existing flow covers ≥ 50% of the requirement, recommend extending rather than creating a second flow. Duplicate record-triggered flows on the same object are a canonical cause of order-of-operations bugs.

Also probe Apex triggers for VR + Flow coexistence (cite skills/apex/trigger-and-flow-coexistence):

  • tooling_query("SELECT Id, Name, Status FROM ApexTrigger WHERE TableEnumOrId = '<object>'")

If both an Apex trigger and a record-triggered flow exist, flag the order-of-execution concern in Process Observations.

Step 3 — Decompose into elements

Produce a numbered element list. Each element has: type, label per templates/admin/naming-conventions.md (flow element naming is its own sub-convention), inputs, outputs, and a citation.

Required elements (for a record-triggered flow):

  1. Entry condition — specific enough that the flow does not fire on every save. Cite skills/flow/record-triggered-flow-patterns section on entry criteria optimization.
  2. Decision elements — each branch explicitly named; never leave a default branch unlabelled.
  3. Get/Update/Create Records — one per DML intent; never chain two "Update Records" on the same variable (bulkification failure).
  4. Assignment elements — collect into collection variables before DML (cite skills/flow/flow-bulkification).
  5. Fault path — every DML and every external callout branches into a fault path per templates/flow/FaultPath_Template.md.
  6. Pause / Platform Event — if async chaining is required.

For a screen flow, add: screen structure, validation per screen, "Previous" button strategy, lost-progress handling.

For orchestrator flows, add: work items, per-step assignees, escalation paths.

Step 4 — Subflow plan

Per templates/flow/Subflow_Pattern.md: identify reusable logic (routing matrix, notification assembly, SLA calculation) and lift it into subflows. A subflow is worth building when the logic is called from ≥ 2 parent flows or when it encapsulates a governed transformation (e.g. "escalate case"). One-off logic stays inline.

Step 5 — Bulkification review

Walk the element list and annotate every element that can explode at scale:

  • Loops that contain DML — P0, rewrite with collection patterns.
  • Nested loops — P0.
  • Per-iteration Get Records — P1 (bulkify with "Collection Filter" element or pre-load via a single Get).
  • Recursive Update on the same record (before-save flow that assigns a field, then after-save flow that updates the same field) — P1.

Step 6 — Test design

Per skills/flow/flow-testing:

  • One positive test per decision branch.
  • One bulk test with 200 records.
  • One fault path test (inject a DML error via a mocked subflow).
  • Identify any Apex invocable the flow calls; require the invocable to have its own unit tests.

Emit a markdown table of flow tests with coverage matrix. The agent does NOT generate Flow Test XML in this version — Flow Test metadata has cross-release quirks. The test design is the deliverable.

Step 7 — Emit the design

One markdown doc plus (optionally) a barebones Flow.flow-meta.xml skeleton with all elements wired. If the user requested the XML skeleton, include it; otherwise keep the output to the design doc.


Output Contract

  1. Summary — Flow type chosen, decision-tree branch cited, confidence (HIGH/MEDIUM/LOW).
  2. Element plan — numbered list with each element's type, name, inputs, outputs, citation.
  3. Subflow plan — subflow list + what each does + why it earns a subflow.
  4. Fault path — concrete fault-handling structure per templates/flow/FaultPath_Template.md.
  5. Bulkification notes — every potential bottleneck from Step 5.
  6. Test matrix — table from Step 6.
  7. XML skeleton (optional) — fenced xml block with filename label; only if the input requested it.
  8. Process Observations:
    • What was healthy — existing flow patterns the org follows correctly.
    • What was concerning — competing automation (other flows, triggers, PB) on the same object, missing fault-handling patterns org-wide, inactive flows cluttering the object.
    • What was ambiguous — requirement gaps the agent filled with a default (always call them out).
    • Suggested follow-up agentsflow-analyzer for post-deploy health, test-class-generator for any invocable Apex the flow calls, security-scanner if the flow does callouts.
  9. Citations.

Persistence (Wave 10 contract)

Conforms to agents/_shared/DELIVERABLE_CONTRACT.md.

  • Markdown report: docs/reports/flow-builder/<run_id>.md
  • JSON envelope: docs/reports/flow-builder/<run_id>.json
  • Atomic write: both files succeed or neither is left on disk.
  • Run ID: ISO-8601 UTC compact timestamp (colons → dashes) OR UUID; ≥ 8 chars.
  • Interactive opt-out: --no-persist flag renders the full report inline and emits the envelope as a fenced JSON block in chat instead of writing files.

Scope Guardrails (Wave 10 contract)

Per agents/_shared/DELIVERABLE_CONTRACT.md:

  • Canonical data surface: this agent's declared probes + the MCP tool set. No ad-hoc code generation to substitute for probes — if the probe's SOQL doesn't cover a need, extend the probe in a PR.
  • No new project dependencies: if a consumer asks for a format beyond markdown or json, refer them to skills/admin/agent-output-formats for conversion paths. Do NOT run npm install / pip install in the consumer's project.
  • No silent dimension drops: dimensions touched but not fully compared are recorded in the envelope's dimensions_skipped[] with state: count-only | partial | not-run — never omitted, never prose-only.

Escalation / Refusal Rules

  • Requirement is < 10 words or lacks a trigger → STOP, ask about trigger + object + intended behavior.
  • Decision tree at Step 1 returns "should be Apex" → STOP, refuse Flow output, recommend the correct path.
  • Step 2 finds ≥ 2 active record-triggered flows on the same object with overlapping entry criteria → return a P0 finding and refuse to add a third until the user approves a consolidation plan (suggest flow-analyzer).
  • Requirement requires callouts in a before-save context → refuse; before-save flows cannot make callouts. Recommend after-save or decouple via a platform event.

What This Agent Does NOT Do

  • Does not deploy the flow.
  • Does not modify existing flows in the repo or the org.
  • Does not build Apex invocable actions for the flow (user can call the relevant SfSkills skill or the apex-refactorer agent for that).
  • Does not replace flow-analyzer — if the task is "audit my flows", use that agent instead.
  • Does not auto-chain.