﻿---
title: Add conditions to workflows
description: Run workflows only for matching events, skip or branch steps, repeat steps, and filter data with KQL conditions in Elastic Workflows. Find where each condition goes and what limits apply.
url: https://www.elastic.co/elastic/docs-builder/docs/5113/explore-analyze/workflows/authoring-techniques/add-conditions-to-workflows
products:
  - Elastic Cloud Enterprise
  - Elastic Cloud Hosted
  - Elastic Cloud Serverless
  - Elastic Cloud on Kubernetes
  - Elastic Documentation
  - Elastic Stack
  - Kibana
applies_to:
  - Elastic Cloud Serverless: Generally available
  - Elastic Stack: Generally available since 9.4
---

# Add conditions to workflows
Conditions decide what a workflow does with the data it receives. With conditions, a workflow runs only for matching events, skips or branches between steps, repeats steps while a check is true, and keeps only the array items that match. Learn where to add each kind of condition, how to write it in KQL, and which length limits apply.

## Decide where to add a condition


| To                                                                                                                                                                                                                | Add the condition to                                                                                                                                                                                                    | It can check                                                                              |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------|
| <applies-to>Elastic Cloud Serverless: Generally available</applies-to> <applies-to>Elastic Stack: Generally available since 9.5, Elastic Stack: Preview in 9.4</applies-to> Run the workflow only for some events | The trigger's `on.condition`                                                                                                                                                                                            | The fields of the trigger's event, such as `event.workflow.name`                          |
| Run or skip one step                                                                                                                                                                                              | The step's [`if`](/elastic/docs-builder/docs/5113/explore-analyze/workflows/steps/if#workflows-step-level-if) parameter                                                                                                 | The workflow's data, such as `inputs`, `consts`, `event`, and the output of earlier steps |
| Run one set of steps or another                                                                                                                                                                                   | An [`if`](https://www.elastic.co/elastic/docs-builder/docs/5113/explore-analyze/workflows/steps/if) step's `condition`                                                                                                  | The workflow's data                                                                       |
| Repeat steps while something is true                                                                                                                                                                              | A [`while`](https://www.elastic.co/elastic/docs-builder/docs/5113/explore-analyze/workflows/steps/while) step's `condition`                                                                                             | The workflow's data, after each iteration                                                 |
| Keep or find the items in an array that match                                                                                                                                                                     | A [`data.filter`](/elastic/docs-builder/docs/5113/explore-analyze/workflows/steps/data#data-filter) or [`data.find`](/elastic/docs-builder/docs/5113/explore-analyze/workflows/steps/data#data-find) step's `condition` | Each item, as `item`, and its position, as `index`                                        |


## Filter which events trigger a workflow

<applies-to>
  - Elastic Cloud Serverless: Generally available
  - Elastic Stack: Generally available since 9.5
  - Elastic Stack: Preview in 9.4
</applies-to>

To run a workflow only for some events, add a KQL query to the trigger's `on.condition`. The query can use any field in the trigger's event, with the `event.` prefix. Without a condition, the workflow runs for every event.
For example, to run the workflow only when a specific workflow fails:
```yaml
triggers:
  - type: workflows.failed
    on:
      condition: "event.workflow.name : 'ops--rollback-deployment'"
```

Kibana checks the condition as written, so it can't include `{{ }}` templating. If Kibana can't evaluate the query, the workflow doesn't run for that event, and Kibana logs a warning in its server log.
To find the fields that each trigger's event includes, refer to [Event-driven triggers](https://www.elastic.co/elastic/docs-builder/docs/5113/explore-analyze/workflows/triggers/event-driven-triggers).
<applies-to>Elastic Cloud Serverless: Generally available</applies-to> <applies-to>Elastic Stack: Generally available since 9.5</applies-to> To check a condition against a real event before you turn the workflow on, refer to [Test a workflow with a real event](/elastic/docs-builder/docs/5113/explore-analyze/workflows/triggers/event-driven-triggers#event-driven-triggers-test).

## Add conditions to steps

Unlike trigger conditions, step conditions can include `{{ }}` values in their KQL. `if` and `while` conditions can also be a `${{ }}` expression that evaluates to a boolean. For how each step uses its condition, refer to these pages:
- [`if` step](/elastic/docs-builder/docs/5113/explore-analyze/workflows/steps/flow-control-steps#if) and [step-level `if`](/elastic/docs-builder/docs/5113/explore-analyze/workflows/steps/if#workflows-step-level-if): Branch between two sets of steps, or skip a single step.
- [`while` step](/elastic/docs-builder/docs/5113/explore-analyze/workflows/steps/flow-control-steps#while): Repeat steps while the condition is true. Kibana checks the condition after each iteration, so the steps always run at least once.
- [`data.filter` and `data.find` steps](/elastic/docs-builder/docs/5113/explore-analyze/workflows/steps/data#data-filter): Keep or find the items in an array that match the condition.


## Write a KQL condition

Every kind of condition accepts a KQL query. The `if` and `while` conditions also accept a `${{ }}` expression that evaluates to true or false. In KQL, use `:` for equality, as in `event.status: "closed"`. For the operators that workflows support, such as ranges, wildcards, and `and`, `or`, and `not`, refer to [Supported KQL features](/elastic/docs-builder/docs/5113/explore-analyze/workflows/steps/if#supported-kql-features).
<note>
  Put quotes around the whole condition, because YAML otherwise reads the `:` in KQL as the beginning of a new key. If the condition contains double quotes, use single quotes around it, as in `condition: 'event.workflow.name: "ops--rollback-deployment"'`.
</note>


## Condition length limits

The maximum length of a condition depends on where you add it:

| Condition                                          | Maximum length                                                                                                                                                                                                                                                                         |
|----------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Trigger `on.condition`                             | Varies by deployment and version:  <applies-to>Elastic Cloud Serverless: Generally available</applies-to> <applies-to>Elastic Stack: Planned</applies-to> 2,000 characters  <applies-to>Elastic Stack: Generally available in 9.5, Elastic Stack: Preview in 9.4</applies-to> No limit |
| Step-level `if` and the `if` step's `condition`    | 2,000 characters. To work around the limit, refer to [Use a condition longer than 2,000 characters](/elastic/docs-builder/docs/5113/explore-analyze/workflows/steps/if#workflows-step-level-if-condition-length).                                                                      |
| `while`, `data.filter`, and `data.find` conditions | No limit                                                                                                                                                                                                                                                                               |