1 export const metadata = {
2 title: `${pageNumber} Conditions in Workflows with When-Then`,
3 }
4
5 # {metadata.title}
6
7 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.
8
9 ## Why If-Conditions Aren't Allowed in Workflows?
10
11 Medusa creates an internal representation of the workflow definition you pass to `createWorkflow` to track and store the workflow's steps. At that point, variables in the workflow don't have any values. They only do when you execute the workflow.
12
13 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.
14
15 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 satisfied.
16
17 <Note title="Tip">
18
19 Restrictions for conditions is only applicable in a workflow's definition. You can still use if-conditions in your step's code.
20
21 </Note>
22
23 ---
24
25 ## How to use When-Then?
26
27 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.
28
29 For example:
30
31 export const highlights = [
32 ["16", "input", "The data to pass as a parameter to the function in the second parameter"],
33 ["18", "return", "The function must return a boolean value indicating whether\nthe callback function passed to `then` should be executed."],
34 ["20", "() => {", "The function to execute if `when`'s second parameter returns a `true` value."]
35 ]
36
37 ```ts highlights={highlights}
38 import {
39 createWorkflow,
40 WorkflowResponse,
41 when,
42 } from "@medusajs/framework/workflows-sdk"
43 // step imports...
44
45 const workflow = createWorkflow(
46 "workflow",
47 function (input: {
48 is_active: boolean
49 }) {
50
51 const result = when(
52 "is-active",
53 input,
54 (input) => {
55 return input.is_active
56 }
57 ).then(() => {
58 const stepResult = isActiveStep()
59 return stepResult
60 })
61
62 // executed without condition
63 const anotherStepResult = anotherStep(result)
64
65 return new WorkflowResponse(
66 anotherStepResult
67 )
68 }
69 )
70 ```
71
72 In this code snippet, you execute the `isActiveStep` only if the `input.is_active`'s value is `true`.
73
74 ### When Parameters
75
76 `when` accepts two parameters:
77
78 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.
79 2. The second parameter is a function that returns a boolean indicating whether to execute the action in `then`.
80 - You can perform conditional checks or use conditional operators in this function, but only on the function's parameters.
81
82 ### Then Callback Function
83
84 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.
85
86 The callback function passed to `then` is treated the same as the workflow's definition function. That means:
87
88 - You can execute steps in the callback function.
89 - You can't [transform data and variables](../variable-manipulation/page.mdx) in the callback function. Instead, use `transform`.
90 - You can't use if-else conditions in the callback function.
91 - You can't use loops in the callback function.
92 - You should keep in mind the [workflow constructor constraints](../constructor-constraints/page.mdx) in the callback function.
93
94 ---
95
96 ## Implementing If-Else with When-Then
97
98 `when-then` doesn't support if-else conditions. Instead, use two `when-then` conditions in your workflow.
99
100 For example:
101
102 export const ifElseHighlights = [
103 ["7", "when", "This when-then block acts as an if condition."],
104 ["16", "when", "This when-then block acts as an else condition."]
105 ]
106
107 ```ts highlights={ifElseHighlights}
108 const workflow = createWorkflow(
109 "workflow",
110 function (input: {
111 is_active: boolean
112 }) {
113
114 const isActiveResult = when(
115 input,
116 (input) => {
117 return input.is_active
118 }
119 ).then(() => {
120 return isActiveStep()
121 })
122
123 const notIsActiveResult = when(
124 input,
125 (input) => {
126 return !input.is_active
127 }
128 ).then(() => {
129 return notIsActiveStep()
130 })
131
132 // ...
133 }
134 )
135 ```
136
137 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.
138
139 ---
140
141 ## Specify Name for When-Then
142
143 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:
144
145 ```ts
146 const isActiveResult = when(
147 input,
148 (input) => {
149 return input.is_active
150 }
151 ).then(() => {
152 return isActiveStep()
153 })
154 ```
155
156 This `when-then` block's internal name will be `when-then-is-active`, where `is-active` is the step's name.
157
158 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.
159
160 You pass a name for `when-then` as a first parameter of `when`, whose signature can accept three parameters in this case. For example:
161
162 export const nameHighlights = [
163 ["2", `"check-is-active"`, "The when-then block's name."],
164 ["10", "return", "`then` returns a value other than the step's result."]
165 ]
166
167 ```ts highlights={nameHighlights}
168 const { isActive } = when(
169 "check-is-active",
170 input,
171 (input) => {
172 return input.is_active
173 }
174 ).then(() => {
175 const isActive = isActiveStep()
176
177 const returnData = transform({
178 isActive,
179 }, (data) => {
180 return {
181 message: data.isActive ? "Active" : "Not Active",
182 }
183 })
184
185 return returnData
186 })
187 ```
188
189 Since `then` returns a value different than the step's result, you pass to the `when` function the following parameters:
190
191 1. A unique name to be assigned to the `when-then` block.
192 2. Either an object or the workflow's input. This data is passed as a parameter to the function in `when`'s second parameter.
193 3. A function that returns a boolean indicating whether to execute the action in `then`.
194
195 The second and third parameters are the same as the parameters you previously passed to `when`.