I have datatype, e.g. Product. I want to send a list of product name and product price for active products in a email body. This is just an example.
product 1, $100
product 3, $150
I can use a recursive workflow to iterate through each product, but how do I construct the “list”. One hack is to store each text, e.g. “product 1, $100” in DB and append the next text to this. Is there a better way not to store in DB?
Yes, there are a few different ways you can construct a list of items in a backend workflow in Bubble without storing the data in the database. Here are a few options:
- Use a string variable to build the list: You can create a string variable in your workflow and use the “Concatenate” action to append new items to the list as you iterate through the products. For example:
- Initialize a string variable called “list” to an empty string
- In the “For each” loop, use the “Concatenate” action to add the product name and price to the “list” variable, separated by a line break or any other desired separator.
- After the loop finishes, the “list” variable will contain the complete list of items. You can use this variable in the email body or any other desired output.
- Use a list variable to build the list: Alternatively, you can use a list variable to store the items in the list. You can then use the “Append to list” action to add each item to the list as you iterate through the products. For example:
- Initialize a list variable called “list” to an empty list
- In the “For each” loop, use the “Append to list” action to add the product name and price to the “list” variable as a single string.
- After the loop finishes, the “list” variable will contain a list of strings, where each string represents a product in the list. You can use this variable in the email body or any other desired output.
- Use a custom function to build the list: If you want to reuse this list building logic in multiple places in your app, you can create a custom function to construct the list. The function can take a list of products as input and return a string containing the list of items. You can then call this function from your backend workflow or any other desired location in your app.
I hope this helps! Let me know if you have any questions or need further assistance with constructing a list of items in a backend workflow in Bubble.
Where can I find the “concatenate” and “append to list” action?
For option 3, I create a recursive API workflow with the following parameters: products (list), index, count, list (list). Where do I find the action “append to list” when I iterate through and build the list?
Please check DM @net-tt. Thanks.
If you just need to format the product list in a text you don’t need workflows or hacks.
You can search you products and then use the operator “format as text” on the list.
Here is an example:
result in a text:
To have a newline as delimiter just hit enter key in the text box of the delimiter.
Your suggestion is for the front end. I need it in a Backend API workflow.
the expression is in the frontend for the sake of the example.
The same operator is available in the backend.
Give it a try and tell me how it goes
That is great. It works! Thanks!
This topic was automatically closed after 70 days. New replies are no longer allowed.