How to used nested for loops in Postman Flows

Hi,
I want to use a Postman Flow to automate a manual task.
The task contains the following actions:
1/ Create a Chapter (POST request)
2/ Create a subtitle for the Chapter (another POST request)

A subtitle can only be create after the creation of a chapter because the chapter id is needed to create the subtitle

The number of chapters and subtitles depends on 2 lists part of the flow.
The first list contains the chapters, the second list contains the subtitles.

Example:
Chapter 1 - SubTitle 1.1, SubTitle 1.2
Chapter 2 - SubTitle 2.1, SubTitle 2.2, SubTitle 2.3

How can I first loop on the list of chapters and for each chapter loop on the list of subtitles?
First create: Chapter 1 - SubTitle 1.1, SubTitle 1.2
Than create: Chapter 2 - SubTitle 2.1, SubTitle 2.2, SubTitle 2.3

Currently I combine 2 for loops but first all chapters are created and after that the subtitles are created.
Chapter 1 - Chapter 2 - SubTitle 1.1, SubTitle 1.2, SubTitle 2.1, SubTitle 2.2, SubTitle 2.3

Any idea how I can achieve this?

Hey @apoleon :wave:

Welcome to the Postman Community! :postman:

I’d love to get @flows-daniel input on this one - He would be able to recommend some good practices here :pray:

Hi @apoleon

Welcome!

You mention expecting the output :

First create: Chapter 1 - SubTitle 1.1, SubTitle 1.2
Than create: Chapter 2 - SubTitle 2.1, SubTitle 2.2, SubTitle 2.3

What is the expected output from the Flow you had above which includes Chapters 1-3 and Subtitles 1-5?

In the example mentioned on the screenshot the expected outcome will be

First step: Chapter 1 - SubTitle 1, SubTitle 2, SubTitle 3 , SubTitle 4 , SubTitle 5
Second step: Chapter 2 - SubTitle 1, SubTitle 2, SubTitle 3 , SubTitle 4 , SubTitle 5
Third step : Chapter 3 - SubTitle 1, SubTitle 2, SubTitle 3 , SubTitle 4 , SubTitle 5

If I run the script now I have Chapter 1, Chapter 2, Chapter 3, SubTitle 1, SubTitle 2, SubTitle 3 , SubTitle 4 , SubTitle 5

Hi @apoleon

To solve the ordering issue see the below flow:

I used placeholder requests but the logic should be the same. First connect the success port of the send request block to the “start” port of the loop so it only runs after the chapter has been created. If timing is important you can add a delay block before/after the middle request to have the data “pause” for a specified amount of time before continuing.

Additionally I believe your flow will get stuck because the MSID variable is being set multiple times and variables currently behave like constants in Flows and aren’t meant to be set multiple times in a flow (we have an open feature request for this).

A workaround to that issue is to add the MSID to every element that’s sent to the second for loop which would give you the following below:

The $map function adds the MSID to every element of the list so instead of passing just “Subtitle 1” it would be a record containing { "subtitle": "Subtitle 1" , "MSID": "msid here"} which can then be selected on the other side.

The Code in the evaluate block is below:

$map(list, fn($e){
    {"subtitle": $e,
    "MSID": msid}
})

Let me know if this works for you or if you have any questions.

2 Likes

Thank you Daniel!!!
My flow works now.
Thanks for the support

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.