Multiple values array processing based on variable input

What are you trying to achieve?

I’m building an automated email bot with AI

My input can have multiple attachment in an array:

{
“result”: {
“result”: true,
“total”: 1,
“confidence”: 100,
“documents”: [
{
“url”: “https://mydoc1.pdf”,
“filename”: “Offer1”
}
]
},

In case I have 1 attachment, i want to downlaod it and add it to my email with HTTP get file feature + add attachment to email feature; that’s easy

but i don’t know the output, 1 or 2 or more files are in the json load
how can I grab all the files and add them all to my email body before finishing the loop

some kind of do while inside a flow? i don’t know if that’s even possible?

If I knew the output was always 1 file or 2 it would be easy but in case its variable it’s difficult for me to setup

the output can also be 0 , no file, and then the flow crashes too :slight_smile:

Welcome to the Make community!

From your output bundle, it appears that you have an ARRAY of items. What do you do when you have an array?

“Looping” Through Array Items

When you SEE an array in your module’s output, think of using an Iterator module.

In this example, responses is an array of items (collections). You’ll want to map this array in an Iterator module.


Question: Have you tried mapping your array variable into an Iterator module, ran the scenario, and view the output?

Next,

Combining Bundles Using Aggregators

Every result (item/record) from trigger/iterator/list/search/match modules will output a bundle. This can result in multiple bundles, which then trigger multiple operations in future modules (one operation per bundle). To “combine” multiple bundles into a single variable, you’ll need to use an aggregator of some sort.

Aggregators are modules that accumulate multiple bundles into one single bundle. An example of a commonly-used aggregator module is the Array aggregator module. The next popular aggregator is the Text Aggregator which is very flexible and can apply to many use-cases like building of JSON, CSV, HTML.

You can find out more about the other types of aggregator modules here:

Question: Which is the best aggregator do you think you’ll need for your use-case?

Mapping a Specific Structure Into a Complex Field

If you have an array of collections, in programming terms, this is called an array of objects, or an array with non-primitive data types (“complex”).

The Array Aggregator module is very powerful because it allows you to build a new complex array of collections that matches a later module’s field to map multiple items (collections) to it. Such fields initially would allow you to manually add items, but you can toggle the “Map” switch to the “on” state and map a whole array into a single field.

This is done by selecting the “Target structure type” in an Array Aggregator module.

As you can see from the above example, the “Map” toggle on complex fields are used when you have an array variable (like from an array aggregator). Other combinations of modules may also allow you to generate an array that matches a future field’s array structure, like “Aggregate to JSON + Parse JSON”, or “Create JSON + Parse JSON”, but this is an advanced topic.

Question: Are you mapping your array into a field that accepts more than one item/collection?

Example

Here is an example of how your scenario could look like:

This is just an example. Your final solution may or may not look like this depending on your requirements and actual data.

For more information, see “Mapping with arrays” in the Help Centre. You should also do the Make Academy, which also covers the use of Iterators & Aggregators.

Hope this helps! Let me know if there are any further questions or issues. P.S.: investing some effort into the tutorials in the Make Academy will save you lots of time and frustration using Make!

@samliew

Hi Tom,

For this you would need to use the built in iterator app in your flow. This would need to be placed after the input has been received. You’ll need to identify the array which would be [documents]. You should then be able to place url and filename datapills in subsequent steps. However, it’s important that you reference the datapills from the iterator(flow control) step not the original output. These datapills are dynamic and will change based on the index of the iteration.

thx this seems like a good solution and i’ll try that;

One question: after the loop 1 want to send 1 email not 3 emails in case there are 3 attachments.

so i want:
iterator → loop eg 3 items → download file → add file to email

and then i want a few other actions (run just once not 3 times)
so continue with the regular flow when the loop is ended.

how can I do that?

question: in this example Make will add each item in the google sheet as a row + sends an email for Each item right?

We want almost the same but send only 1 email not for each item

Yes, put an aggregator module between “download file → add file to email”.

For more information, see provided links above.

Hope this helps! Let me know if there are any further questions or issues. P.S.: investing some effort into the tutorials in the Make Academy will save you lots of time and frustration using Make!

@samliew