In the context of a process API, “loops” refer to iterating over a collection of items. Consider a scenario where you have a list of records and your goal is to process each record individually. The process involves the following steps:
- Looping Through Records: You iterate over each record in your list. This can be achieved using a for-loop or any other iterative structure suitable for your programming environment.
- Performing Checks and Operations: For each record, you evaluate a boolean expression. If this expression evaluates to true, you perform certain operations on the record. These operations might include:
- Checking conditions: Evaluating whether the record meets specific criteria.
- Updating Records: Modifying the record based on certain conditions or data.
- Making Server Calls: Interacting with external APIs or servers to fetch or send data related to the record.
- Adding to a Different List: If the record passes your boolean expression, you add it to a separate list. This list will be used for exporting or further processing at the end of your operations.
- Exporting or Further Processing: Once all records have been processed, you can export this new list or perform additional steps as required.
It’s important to note that loops in a process API context are versatile and can be used for a wide range of operations beyond simple checks, including data manipulation, API interactions, and complex logical operations. The specific implementation details will depend on the programming language and environment you are using.
The intent type is loop, but in this case looping is also the action so we don’t define a action. This is an example of the loop step structure.
...
"steps": {
"start": {
"type": "loop",
"args": {
"source": "$parameters.records",
"steps": {
"start": {
...
"next_step": "next_task"
},
"next_taks": {
...
}
}
},
"next_step": "save_new_collection"
},
"save_new_collection": {
... perform step ...
}
}
In the above example you will notice that the args object has two properties:
- source: This is the collection that you’ll iterate over during the loop.
- steps: This is a dictionary defining the operations to be executed during each iteration of the loop. It can consist of either a single step or a sequence of steps. The sequence is structured according to standard step definition patterns, which often include specifying the “next_step” to dictate the flow of operations.
In the provided example, the loop includes a “next_step” (“save_new_collection”) that is executed after the loop completes. This means the loop step functions similarly to any other step in the process. The key distinction is that during the loop, the current item being iterated over is designated as the “item” parameter in the actions. Additionally, this item can be accessed using the “$item” expression in the arguments of each step.
{
"id": "simple_loop_example",
"main": {
"parameters_def": {
"records": { "type": "array", "required": true }
},
"steps": {
"start": {
"type": "loop",
"steps": {
"start": {
"type": "console",
"action": "log",
"args": {
"messages": ["$item.code", "$item.cost"]
}
}
}
}
}
}
}