Conditions

Learn how Knock's conditions model provides dynamic control flow to your workflow runs.

Knock uses conditions to model checks that determine variations in your workflow runs. They provide a powerful way to create more advanced notification logic flows.

You can use conditions in three areas of the Knock model:

  1. Step conditions — Used to determine if a single step in one of your workflows should execute during each workflow run. For example, only send an email if the preceding in-app notification has not yet been read or seen.
  2. Channel conditions — Used to determine if any step using the given channel should execute across all workflow runs. For example, only execute your Postmark email channel steps in your production environment.
  3. Preference conditions — Used to determine the complete set of preferences available to the current workflow run. For example, allow a recipient to mute notifications for specific resources in your product.

Each of these three cases share the same underlying data model and UI editor, which we outline in detail here.

Condition types

Knock's shared conditions model supports the following types of conditions:

  • Data — Evaluates against a property in the workflow trigger data payload.
  • Recipient — Evaluates against a property on the workflow run recipient.
  • Actor — Evaluates against a property on the workflow run actor.
  • Environment variable — Evaluates against one of your environment variables.
  • Workflow — Evaluates against a property of the currently executing workflow.
  • Workflow run state — Evaluates against a property of the current workflow run.
  • Tenant — Evaluates against a property on the tenant associated with the current workflow run.
  • Message status — Evaluates against the delivery status or engagement status of a message from a previous step in the current workflow run.
💡
Message status conditions are only available when designing step-level conditions. They are not available for use with channel-level or preference-level conditions. You can learn more about how to work with message status conditions in our guide on step-level conditions.

Modeling conditions

Knock models each condition as a combination of three properties: a variable, an operator, and an argument. This will feel familiar to boolean logic with infix operators in many modern programming languages.

In our JSON representation of a workflow this will look something like:

We also provide a conditions editor that provides some helpful UX abstractions on top of this model for building conditions in the Knock dashboard.

Variables

A condition variable is always a string formatted like "<prefix>.<path>". Knock uses the variable prefix to determine the condition type and the variable path to determine where to look up the data for evaluation.

See the conditions scope for a list of available prefixes.

Arguments

Knock uses the condition argument as the expected value in the condition evaluation. Arguments can be either static values or dynamic properties.

Static arguments

Static arguments can be any of the following JSON literals:

  • Strings ("foo", "bar", "baz")
  • Numbers (1.0, 2, 10000)
  • Booleans (true, false)
  • null

Plus arrays of any of the above.

Dynamic arguments

Dynamic arguments are nearly identical to variables. Knock will expect a string formatted like "<prefix>.<path>" and use the information within to resolve a value from some runtime data property.

See the conditions scope for a list of available prefixes.

Operators

You can use any of the following operators in condition comparisons:

OperatorDescription
equal_to==
not_equal_to!=
greater_than>
greater_than_or_equal_to>=
less_than<
less_than_or_equal_to<=
containsargument in variable (works with strings and lists)
contains_allare all argument in variable (works with single arguments, or lists of arguments)
not_containsargument not in variable (works with strings and lists)
emptyvariable in ["", null, []]
not_emptyvariable not in ["", null, []]

Note: the empty and not_empty operators do not require a companion argument value in the condition, since Knock is checking for the absence of data from the variable path.

Conditions scope

Knock makes the following available to be used in a condition variable or dynamic argument:

PropertyDescription
data.<path>A data condition, where <path> is used to select a property from the workflow trigger data payload.
recipient.<prop>A recipient condition, where <prop> is used to select a property on the current recipient. See full list of properties available.
actor.<prop>An actor condition, where <prop> is used to select a property on the current actor. See full list of properties available.
vars.<var>An environment variable condition, where <var> is the name of one of your environment variables.
workflow.{id,name,categories}A workflow condition.
run.{total_activities,total_actors}A workflow run condition.
tenant.<prop>A tenant condition, where <prop> is used to select a property on the current tenant. See full list of properties available.
refs.<ref>.delivery_statusA message status condition that evaluates against a message's delivery status, where <ref> identifies the preceding workflow step that generated the message.
refs.<ref>.engagement_statusA message status condition that evaluates against a message's engagement status, where <ref> identifies the preceding workflow step that generated the message.

In cases where data is not found at the path given by the variable, Knock falls back to an empty string as the default value.

Combining conditions

You can combine multiple conditions together via either AND or OR operators.

  • AND combined conditions require all conditions to be true for the evaluation to pass.
  • OR combined conditions require at least one condition to be true for the evaluation to pass.
  • You may also use a combination of AND and OR operators to create more complex conditions.

The conditions editor

The Knock Dashboard ships with a conditions editor that provides helpful abstractions on top of this data model. Rather than needing to remember how to format variables or name operators, Knock makes the appropriate options available to you.

When creating or modifying a condition, you'll see:

  • A dropdown to select the condition type. Knock will use this option to determine the variable <prefix> value.
  • An input or dropdown to provide the variable data path.
  • A dropdown to select the operator.
  • An input or dropdown to provide the argument data path.

Working with the conditions editor to build a recipient data condition.

Working with the conditions editor to build a recipient data condition.

You can also use the conditions editor to combine multiple conditions together via either AND or OR operators.

Managing condition groups in the conditions editor.

Managing condition groups in the conditions editor.

The condition editor is available for use in the workflow step editor and the channel environment settings editor.

Debugging conditions

Knock executes any step, channel, and preference conditions for each step within a workflow run. As part of execution, Knock captures detailed information about each condition evaluation for use in the workflow debugger.

Debugging step and channel conditions

Knock will display step and channel conditions evaluation results together in the step detail panel in the debugger. The overall evaluation result will show whether the step was skipped. For each individual condition within the set, Knock will show either:

  1. The condition evaluation result. This will include any dynamically resolved variable and argument data captured at workflow run time.
  2. A "not evaluated" state. This will occur when a preceding condition or group has determined the result, meaning subsequent conditions did not require full evaluation.

Debugging step and channel conditions.

Debugging step and channel conditions.

Debugging preference conditions

Knock will display any preference conditions evaluations just below the step and channel conditions results. Knock will group each condition evaluation by location within the resolved preference set. The overall evaluation result will show whether the recipient opted-out for the given workflow, category, or channel type.

Debugging preference conditions.

Debugging preference conditions.