3.6.2. Conditions in Workflows with When-Then

In this chapter, you'll learn how to execute an action based on a condition in a workflow using when-then from the Workflows SDK.

Why If-Conditions Aren't Allowed in Workflows?#

Medusa creates an internal representation of the workflow definition you pass to createWorkflow to track and store its steps. At that point, variables in the workflow don't have any values. They only do when you execute the workflow.

So, you can't use an if-condition that checks a variable's value, as the condition will be evaluated when Medusa creates the internal representation of the workflow, rather than during execution.

Instead, use when-then from the Workflows SDK. It allows you to perform steps in a workflow only if a condition that you specify is satisified.


How to use When-Then?#

The Workflows SDK provides a when function that is used to check whether a condition is true. You chain a then function to when that specifies the steps to execute if the condition in when is satisfied.

For example:

Code
1import { 2  createWorkflow,3  WorkflowResponse,4  when,5} from "@medusajs/framework/workflows-sdk"6// step imports...7
8const workflow = createWorkflow(9  "workflow", 10  function (input: {11    is_active: boolean12  }) {13
14    const result = when(15      input, 16      (input) => {17        return input.is_active18      }19    ).then(() => {20      const stepResult = isActiveStep()21      return stepResult22    })23
24    // executed without condition25    const anotherStepResult = anotherStep(result)26
27    return new WorkflowResponse(28      anotherStepResult29    )30  }31)

In this code snippet, you execute the isActiveStep only if the input.is_active's value is true.

When Parameters#

when accepts the following parameters:

  1. The first parameter is either an object or the workflow's input. This data is passed as a parameter to the function in when's second parameter.
  2. The second parameter is a function that returns a boolean indicating whether to execute the action in then.

Then Parameters#

To specify the action to perform if the condition is satisfied, chain a then function to when and pass it a callback function.

The callback function is only executed if when's second parameter function returns a true value.


Implementing If-Else with When-Then#

when-then doesn't support if-else conditions. Instead, use two when-then conditions in your workflow.

For example:

Code
1const workflow = createWorkflow(2  "workflow", 3  function (input: {4    is_active: boolean5  }) {6
7    const isActiveResult = when(8      input, 9      (input) => {10        return input.is_active11      }12    ).then(() => {13      return isActiveStep()14    })15
16    const notIsActiveResult = when(17      input,18      (input) => {19        return input.is_active20      }21    ).then(() => {22      return notIsActiveStep()23    })24
25    // ...26  }27)

In the above workflow, you use two when-then blocks. The first one performs a step if input.is_active is true, and the second performs a step if input.is_active is false, acting as an else condition.


Specify Name for When-Then#

Internally, when-then blocks have a unique name similar to a step. When you return a step's result in a when-then block, the block's name is derived from the step's name. For example:

Code
1const isActiveResult = when(2  input, 3  (input) => {4    return input.is_active5  }6).then(() => {7  return isActiveStep()8})

This when-then block's internal name will be when-then-is-active, where is-active is the step's name.

However, if you need to return in your when-then block something other than a step's result, you need to specify a unique step name for that block. Otherwise, Medusa will generate a random name for it which can cause unexpected errors in production.

You pass a name for when-then as a first parameter of when, whose signature can accept three parameters in this case. For example:

Code
1const { isActive } = when(2  "check-is-active",3  input, 4  (input) => {5    return input.is_active6  }7).then(() => {8  const isActive = isActiveStep()9
10  return {11    isActive,12  }13})

Since then returns a value different than the step's result, you pass to the when function the following parameters:

  1. A unique name to be assigned to the when-then block.
  2. Either an object or the workflow's input. This data is passed as a parameter to the function in when's second parameter.
  3. A function that returns a boolean indicating whether to execute the action in then.

The second and third parameters are the same as the parameters you previously passed to when.

Was this chapter helpful?
Edit this page