# Need to run request multiple times in a flow for setup

**URL:** <https://community.postman.com/t/need-to-run-request-multiple-times-in-a-flow-for-setup/53219>\
**Category:** Help Hub\
**Tags:** collections\
**Created:** [October 13, 2023, 5:14am UTC](https://community.postman.com/t/need-to-run-request-multiple-times-in-a-flow-for-setup/53219 "2023-10-13T05:14:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![autoguardias](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@autoguardias](https://community.postman.com/u/autoguardias)\
**Post date:** [October 13, 2023, 5:14am UTC](https://community.postman.com/t/need-to-run-request-multiple-times-in-a-flow-for-setup/53219/1 "2023-10-13T05:14:08Z")

</div>

My end goal is to be able to call a particular request multiple times within one workflow.  
Say for example I want create a set of users such that I can call more advanced requests on them.  
I need to perform an initial setup (ONLY ONCE) in my collection run and a final clean up (ONLY ONCE).  
My initial thought was to do it with pre-request script on the collection but turns out that that code is executed at for every request (which makes absolutely no sense to me of that design choice, if someone could explain).  
So I am trying to see if I can include a same request multiple times in a collection but that is also not an option (which again makes me think that either I am missing out on something important conceptually or I really don’t understand why that design choice )

Thank you for your help.

---

<div class="post-metadata">

**Author:** ![xk0der](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/xk0der/32/2056_2.png) [@xk0der](https://community.postman.com/u/xk0der)\
**Post date:** [October 13, 2023, 5:43am UTC](https://community.postman.com/t/need-to-run-request-multiple-times-in-a-flow-for-setup/53219/2 "2023-10-13T05:43:03Z")

</div>

Hey @autoguardias, welcome to the community! 👋

I wasn’t able to follow your use-case 😅! Would you be able to provide more context? Screenshots, sample code etc wold help. Also not sure if you are taking about [Postman Flows](https://learning.postman.com/docs/postman-flows/gs/flows-overview/)? Do clarify.

In the meanwhile, have you had a look at `postman.setRequest()` to control flow of requests: [Customize request order in a collection run | Postman Learning Center](https://learning.postman.com/docs/collections/running-collections/building-workflows/)

Also, check `pm.sendRequest()` to run a request directly within a script : [Manage API data and workflows using Postman JavaScript objects | Postman Learning Center](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#sending-requests-from-scripts)

`pre-request` as the name signifies, is the script that runs before _every_ request, and is used to programatically control aspects of the request about to be sent. You can use `pm.info` object to get `iteration` information, when running your collection using runner.

Example script that you can put in your Collection level pre-request:

```auto
if(pm.info.iteration === 0) {
    // Run code that you want to run once!
    console.log('Setup')
}

```

And in your last request’s test script add the following to clean-up

```auto
if(pm.info.iteration === (pm.info.iterationCount - 1)) {
    // Your clean-up code goes here
    console.log('Clean-up')
}

```

You can put the clean-up code at Collection level too, inside “Test”, but you would then need to check if the current request is the last one

```auto
if(pm.info.iteration === (pm.info.iterationCount - 1) 
     && pm.info.requestName === "<Your Last Request's Name>") {
    // Your clean-up code goes here
    console.log('Clean-up')
}

```

Checkout Postman’s Sandbox Scripting reference here for more ideas: [Manage API data and workflows using Postman JavaScript objects | Postman Learning Center](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [October 13, 2023, 9:53am UTC](https://community.postman.com/t/need-to-run-request-multiple-times-in-a-flow-for-setup/53219/3 "2023-10-13T09:53:07Z")

</div>

Without seeing more details on the flow you are trying to achieve, I would recommend that your setup and tear down requests are proper requests.

You can use sendRequest() in a pre-request script, but I don’t think that will be needed in this scenario (and in my opinion it makes it harder to read and troubleshoot). It’s much better if you can run this using full requests.

You then want to loop on a request (or set of requests) that are in the middle of the collection.

This is done using setNextRequest() and a useful JavaScript function called array.shift().

Please note, setNextRequest() only works with the collection runner.

If you had 3 requests that you wanted to loop through.

- You would store the array of users in a collection or environment variable.
- Please use JSON.stringify() when storing arrays, and JSON.parse() when retrieving.
- The first request would have a pre-request script that retrieves the array of test data\users.
- Using array.shift(), this will retrieve the first value from the array, and then delete it from the array.
- You will then save the current value to a new variable that can be used in the request.
- You will also re-save the array collection variable (which will now have one less record).
- In the tests tab for the 3rd request (or in the same request if its only one request). You will retrieve the array again. At the end of the tests tab, you will have an IF statement that checks the size of the array, and if its more than \> zero, you will use setNextRequest() to run the first request in the set of 3 again.
- If the size of array is zero, then it should then move onto the tear down script.

Just a bit of advice on tear down scripts. It’s better to check for the known state in the startup script, than rely on the tear down script from a previous run to have completed.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [November 12, 2023, 9:53am UTC](https://community.postman.com/t/need-to-run-request-multiple-times-in-a-flow-for-setup/53219/4 "2023-11-12T09:53:18Z")

</div>

Closed on November 12, 2023, 9:53am UTC

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