@@ -31,6 +31,7 @@ Provides Serverless Workflow language examples
3131- [ Filling a glass of water (Expression functions)] ( #Filling-a-glass-of-water )
3232- [ Online Food Ordering] ( #Online-Food-Ordering )
3333- [ Continuing as a new Execution] ( #Continuing-as-a-new-Execution )
34+ - [ Process Transactions (Foreach State with conditions)] ( #Process-Transactions )
3435
3536### Hello World Example
3637
@@ -4588,4 +4589,125 @@ functions:
45884589
45894590</td>
45904591</tr>
4591- </table>
4592+ </table>
4593+
4594+ # ## Process Transactions
4595+
4596+ # ### Description
4597+
4598+ This example shows how we can loop through a data input array (in parallel), and decide which action to perform
4599+ depending on the value of each element in the input array.
4600+ We use the [action definition](../specification.md#Action-Definition) `condition` property to perform the action that
4601+ is best suited for the transaction value.
4602+ Note that in this example we set the "large transaction amount" as a [workflow constant](../specification.md#Workflow-Constants).
4603+ There are other ways to set
4604+ this value, for example passing it as [workflow data input](../specification.md#Workflow-Data-Input),
4605+ or if this data is sensitive, to use [workflow secrets](../specification.md#Workflow-Secrets).
4606+
4607+ For the example, we assume the following workflow data input :
4608+
4609+ ` ` ` json
4610+ {
4611+ "customer": {
4612+ "id": "abc123",
4613+ "name": "John Doe",
4614+ "transactions": [1000, 400, 60, 7000, 12000, 250]
4615+ }
4616+ }
4617+ ` ` `
4618+
4619+ We use the [ForeEach workflow state](../specification.md#ForEach-State) to iterate through customer transactions (in parallel), and
4620+ decide which activity to perform based on the transaction value.
4621+
4622+ # ### Workflow Definition
4623+
4624+ <table>
4625+ <tr>
4626+ <th>JSON</th>
4627+ <th>YAML</th>
4628+ </tr>
4629+ <tr>
4630+ <td valign="top">
4631+
4632+ ` ` ` json
4633+ {
4634+ "id": "customerbankingtransactions",
4635+ "name": "Customer Banking Transactions Workflow",
4636+ "version": "1.0",
4637+ "specVersion": "0.7",
4638+ "autoRetries": true,
4639+ "constants": {
4640+ "largetxamount" : 5000
4641+ },
4642+ "states": [
4643+ {
4644+ "name": "ProcessTransactions",
4645+ "type": "foreach",
4646+ "inputCollection": "${ .customer.transactions }",
4647+ "iterationParam": "${ .tx }",
4648+ "actions": [
4649+ {
4650+ "name": "Process Larger Transaction",
4651+ "functionRef": "Banking Service - Larger Tx",
4652+ "condition": "${ .tx >= $CONST.largetxamount }"
4653+ },
4654+ {
4655+ "name": "Process Smaller Transaction",
4656+ "functionRef": "Banking Service - Smaller Tx",
4657+ "condition": "${ .tx < $CONST.largetxamount }"
4658+ }
4659+ ],
4660+ "end": true
4661+ }
4662+ ],
4663+ "functions": [
4664+ {
4665+ "name": "Banking Service - Larger Tx",
4666+ "type": "asyncapi",
4667+ "operation": "banking.yaml#largerTransation"
4668+ },
4669+ {
4670+ "name": "Banking Service - Smaller T",
4671+ "type": "asyncapi",
4672+ "operation": "banking.yaml#smallerTransation"
4673+ }
4674+ ]
4675+ }
4676+ ` ` `
4677+
4678+ </td>
4679+ <td valign="top">
4680+
4681+ ` ` ` yaml
4682+ id: bankingtransactions
4683+ name: Customer Banking Transactions Workflow
4684+ version: '1.0'
4685+ specVersion: '0.7'
4686+ autoRetries: true
4687+ constants:
4688+ largetxamount: 5000
4689+ states:
4690+ - name: ProcessTransactions
4691+ type: foreach
4692+ inputCollection: "${ .customer.transactions }"
4693+ iterationParam: "${ .tx }"
4694+ actions:
4695+ - name: Process Larger Transaction
4696+ functionRef: Banking Service - Larger Tx
4697+ condition: "${ .tx >= $CONST.largetxamount }"
4698+ - name: Process Smaller Transaction
4699+ functionRef: Banking Service - Smaller Tx
4700+ condition: "${ .tx < $CONST.largetxamount }"
4701+ end: true
4702+ functions:
4703+ - name: Banking Service - Larger Tx
4704+ type: asyncapi
4705+ operation: banking.yaml#largerTransation
4706+ - name: Banking Service - Smaller T
4707+ type: asyncapi
4708+ operation: banking.yaml#smallerTransation
4709+ ` ` `
4710+
4711+ </td>
4712+ </tr>
4713+ </table>
0 commit comments