Skip to content

πŸ”€ If/Else NodeΒ #1265

Closed
twentyhq/twenty
#16833

Description

@vickywxng

Description:

If/else node makes sure that workflow always continues with the path changing depending on the condition.

  • Default behavior: If/else nodes split into two paths β†’ If (true) or Else (false).
  • Else if: Add more routes; conditions are checked in order, and the first match is taken.
  • Else branch will always exist and takes care of all the other cases not specified (does not need to be configured).
  • At runtime, the flow will follow only one path.

Key difference from filters:

  1. Filters: Gatekeepers meaning workflow may stop if the condition fails.
  2. If/else: Always continues, just down different paths.

Figma file: https://www.figma.com/design/xt8O9mFeLl46C5InWwoMrN/Twenty?node-id=76218-40586&t=u6a2zdcUZjY6ziCO-1

Default state when if/else node is added:
Image

When additional else if route is added:
Image

Technical Strategy

Frontend - Step content

The frontend structure will be similar to the Filter Action. Check component WorkflowEditActionFilter
For filters we have:

  • an array of filter groups. Check StepFilters.ts
export type StepFilterGroup = {
  id: string;
  logicalOperator: StepLogicalOperator;
  parentStepFilterGroupId?: string;
  positionInStepFilterGroup?: number;
};
  • an array of filters. Each filters being linked to a group.

We will re-use the structure here. We should simply add a new one:

type StepIfElseBranch = {
  id: string;
  nextStepIds: string[];
  filterGroupId?: string;  // ONE group per branch. ELSE branch = undefined
};

The filter group position will determine the if/else operand. First one is IF, middles are ELSE_IF, last one is ELSE.

Backend - Step content

If-Else should be a similar logic to FilterWorkflowAction. Main difference is:

  • filter action executes all groups and filters at once and returns a boolean `matchesFilter``
  • If-Else will execute its branches - and the associated filters - one by one. Once a branche returns matchesFilter, the step will return the nextStepIds associated to the branch.
  • ELSE branch handling: The ELSE branch should have no filters (or empty filter groups). When no previous branch matches, the ELSE branch is selected by default.
  • the result of the step will be matchingBranchId

Backend - Routing

Since the matchingBranchId will be stored in step result, the workflow executor will be able to retrieve the next step ids to execute.
We simply need to update getNextStepIdsToExecute. If the executed step is IfElse, then we look for the matchingBranchId in result. Then, in the settings, retrieve the nextStepIds linked to that branch.

async getNextStepIdsToExecute({
  executedStep,
  executedStepResult,
}: {
  executedStep: WorkflowAction;
  executedStepResult: WorkflowActionOutput;
}): Promise<string[] | undefined> {
  // ... existing iterator logic ...

  const isIfElseStep = isWorkflowIfElseAction(executedStep);

  if (isIfElseStep) {
    const ifElseResult = executedStepResult.result as
      | WorkflowIfElseResult
      | undefined;

    if (ifElseResult?.matchingBranchId) {
      const matchingBranch = executedStep.settings.input.branches.find(
        (branch) => branch.id === ifElseResult.matchingBranchId
      );
      return matchingBranch?.nextStepIds;
    }
  }

  return executedStep.nextStepIds;
}

Frontend - Routing

This may be the trickiest part. Behavior:

  • Single handle at bottom for all branch edges
  • No step-level nextStepIds β€” routing lives in branches[].nextStepIds
  • On creation: Auto-creates IF + ELSE branches, each with an empty node
  • IF/ELSE branches: Mandatory, cannot be deleted
  • ELSE-IF branches: Added/deleted via step settings UI, creates empty node on add, orphans steps on delete
  • Edges: Non-editable (not deletable, movable, or reconnectable) β€” controlled entirely by branch settings
  • Labels: if / else if / else

What should be updated:

  1. Diagram Generation. Create generateNodesAndEdgesForIfElseNode.ts. Loop through branches, create one edge per branch.nextStepIds entry. Set deletable: false, selectable: false, reconnectable: false. Add label based on branch position (first = "if", last = "else", middle = "else if")

  2. Node Creation (Backend). When creating If/Else step, auto-create 2 empty nodes - see how this is done for iterators. Initialize with IF branch with filterGroupId + ELSE branch without filterGroupId. Return additionalCreatedSteps like Iterator does in runStepCreationSideEffectsAndBuildStep

  3. Branch Management (Frontend UI)

  • Add ELSE-IF: Insert branch before ELSE, create empty node, shift positions
  • Delete ELSE-IF: Remove branch from array, orphan connected steps
  • No edge creation/deletion APIs used β€” all changes go through step settings update
  1. No changes to edge hooks
  • useCreateEdge / useDeleteEdge not used for If/Else edges
  • Edge existence derived from branch settings at render time

Not sure about the full start. This is a global idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Dev status

    None yet

    Start date

    None yet

    Target date

    None yet

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions