How to loop through all items of a table and perform an action
To loop through all items of a table and perform an action, follow these steps using the example of a table called Products with an empty field called SKU, which we will be filling with a value created by combining the word NEW with the ID of the particular item:
-
Open the Products table, click on Options, and then select Automations and New Flow.
-
Add a flow name, such as Create SKU.
-
Select a trigger. We will use: Manually by Flow or Button.
-
Leave the IF section blank.
-
In the THEN section, do the following:
-
Drag a Search Loop block from the Logic section of the left sidebar. This block lets you go through every item in the table conditionally.
-
In this case, we want to iterate through all products where the SKU field is empty.
-
To achieve this, we can set the condition to Where SKU contains [empty value].
-
We need to select our Products table from the Table option.
-
The SKU field should be selected from the Where option.
-
The condition should be set to Contains.
-
The Value field can be left empty since we do not need to search for a specific value.
-
-
Create a variable for the SKU value by dragging a Calculate Variable block from the "Logic" section of the left sidebar.
-
Give the variable a name, such as sku, and write a formula for creating the SKU.
-
Write a formula for creating the SKU using PHP syntax.
-
The formula should concatenate the word NEW with the ID of the product that is currently active in the loop.
-
Use the @ symbol to access the ID, which provides two options:
-
Current - for the item in which the flow is triggered
-
Products - for the item currently active in the loop.
-
-
The formula should be "NEW"+@{Products: ID}.
-
-
Drag an Update Record block from the Actions section of the left sidebar. This block lets you update an item.
-
Select the Record as Current Products Record
-
This Products Record is the item in which the flow is triggered
-
Current Products Record is item currently active in the loop
-
-
The SKU field as the field to update
-
Input @{Variable: sku} to select the variable created earlier as the new value for the SKU field.
-
-
It is always a good practice to perform calculations as a variable and use the variable in the action field instead of performing calculations directly in the action field.
-
Save the flow.
-
Click Edit and then Simulate Variables to test the flow without executing changes on the items. This is a helpful step to ensure that the flow works correctly before making any changes to the actual items.
-
Finally, click Run and enter any item ID to execute the flow and fill the SKU field with the newly created values.