# How can I add the loop?

**URL:** <https://community.postman.com/t/how-can-i-add-the-loop/60976>\
**Category:** Help Hub\
**Tags:** loop\
**Created:** [May 14, 2024, 1:12am UTC](https://community.postman.com/t/how-can-i-add-the-loop/60976 "2024-05-14T01:12:25Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![docking-module-geol7](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@docking-module-geol7](https://community.postman.com/u/docking-module-geol7)\
**Post date:** [May 14, 2024, 1:12am UTC](https://community.postman.com/t/how-can-i-add-the-loop/60976/1 "2024-05-14T01:12:25Z")

</div>

Version - Postman 11

I have one of the request, I wanted to run 10 times..

1st request /Get - AUthenticate once

2nd Request / Post - I wanted to run 10 times..

CreateOrder - I got the body and Pre scripts as follows -

```auto
var currentCount = pm.environment.get("count");
var i =0;
while (i<=currentCount)
{
postman.setNextRequest("CreateOrder ");
}

```

Its not working..any help is appreciated?

---

<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:** [May 14, 2024, 9:23am UTC](https://community.postman.com/t/how-can-i-add-the-loop/60976/2 "2024-05-14T09:23:11Z")

</div>

setNextRequest() just sends the next request that will run after the current request and all of the code in the pre-request scripts and tests tab have completed.

Therefore you cannot use a while loop to control this.

Before we start providing code and example, can you please search the forum as this question has been asked plenty of times and there are quite a few examples out there.

Try one of those, and if you are still having issues, please post your code and an example response.

Please remember to use the preformatted text option in the editor when pasting code or JSON (to stop everything from being aligned to the left).

---

<div class="post-metadata">

**Author:** ![docking-module-geol7](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@docking-module-geol7](https://community.postman.com/u/docking-module-geol7)\
**Post date:** [May 14, 2024, 1:19pm UTC](https://community.postman.com/t/how-can-i-add-the-loop/60976/3 "2024-05-14T13:19:45Z")

</div>

I found lot of links but none worked for me.

---

<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:** [May 14, 2024, 1:35pm UTC](https://community.postman.com/t/how-can-i-add-the-loop/60976/4 "2024-05-14T13:35:03Z")

</div>

Share an example response, and I’ll show you how to develop an appropriate loop in a pre-request script.

---

<div class="post-metadata">

**Author:** ![docking-module-geol7](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@docking-module-geol7](https://community.postman.com/u/docking-module-geol7)\
**Post date:** [May 14, 2024, 1:49pm UTC](https://community.postman.com/t/how-can-i-add-the-loop/60976/5 "2024-05-14T13:49:25Z")

</div>

1st request - Authenticate. - need to run only once. (1st request is done and working)  
2nd request - CreateOrder - need to run 10 times…

I have added the environment variable count…

coming to 2nd request–

**Pre-script**  
var currentCount = pm.environment.get(“count”);

**Body:**  
Actual logic

**Post-response:**  
pm.test(“Status code is 201”, function () {  
pm.response.to.have.status(201);  
});

if (currentCount \> 0) {  
currentCount = currentCount - 1;  
pm.execution.setNextRequest("CreateOrder ");  
pm.environment.set(“count”, currentCount);  
}  
else {  
pm.execution.setNextRequest(null);  
}

script run only once.  
any help is appreciated.

---

<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:** [May 14, 2024, 2:21pm UTC](https://community.postman.com/t/how-can-i-add-the-loop/60976/6 "2024-05-14T14:21:23Z")

</div>

Are you running this from the collection runner.

setNextRequest() only works with the collection runner.

On a side note, all of your code can be in the pre-request script as it sets the next request that will run after the request and the code in the pre-request scripts and tests tab has completed.

In relation to troubleshooting, please get used to console logging the variables.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [May 14, 2024, 2:22pm UTC](https://community.postman.com/t/how-can-i-add-the-loop/60976/7 "2024-05-14T14:22:21Z")

</div>

Hey @docking-module-geol7 👋

Please wrap your code blocks with backticks (```) - It makes it so much easier for folks to read it. 🙏

This:  
pm.test(“Status code is 201”, function () {  
pm.response.to.have.status(201);  
});

if (currentCount \> 0) {  
currentCount = currentCount - 1;  
pm.execution.setNextRequest("CreateOrder ");  
pm.environment.set(“count”, currentCount);  
}  
else {  
pm.execution.setNextRequest(null);  
}

vs this:

```javascript
pm.test(“Status code is 201”, function () {
    pm.response.to.have.status(201);
});

if (currentCount > 0) {
    currentCount = currentCount - 1;
    pm.execution.setNextRequest("CreateOrder ");
    pm.environment.set(“count”, currentCount);
}
else {
    pm.execution.setNextRequest(null);
}

```

---

<div class="post-metadata">

**Author:** ![docking-module-geol7](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@docking-module-geol7](https://community.postman.com/u/docking-module-geol7)\
**Post date:** [May 14, 2024, 2:46pm UTC](https://community.postman.com/t/how-can-i-add-the-loop/60976/8 "2024-05-14T14:46:08Z")

</div>

Thanks,

I am not using the collection runner as once its working, I am going to use Newman CLI.

---

<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:** [May 14, 2024, 2:50pm UTC](https://community.postman.com/t/how-can-i-add-the-loop/60976/9 "2024-05-14T14:50:43Z")

</div>

Here is my effort.

This can all be done in the pre-request script.

```auto
x = 10; // number of iterations

if (typeof array === 'undefined' || array.length == 0) {
    array = Array.from({length: x}, (_, i) => i + 1)
    pm.environment.unset("currentCount");
}

let count = array.shift();
pm.environment.set("currentCount", count);

if (array.length > 0) {  
    currentRequest = pm.info.requestName;
    pm.execution.setNextRequest(currentRequest);
} 

```

Running against a simple GET request with a query parameter.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/0/c03627b8bdb9d3bfcfbe32c45369a4b96441047a.png)

With the corresponding console logs.

![image](https://us1.discourse-cdn.com/getpostman/original/3X/2/9/29d98f69e1600a87e2ed331addc885c97ed74d9f.png)

If you don’t need to use the counter elsewhere, you could potentially just use a local variable to control the loop like the following.

This works because local variables will persist throughout the collection run.

```auto
x = 10;

if (typeof array === 'undefined' || array.length == 0) {
    array = Array.from({length: x}, (_, i) => i + 1)
}

pm.variables.set("currentCount", array.shift())

if (array.length > 0) {  
    currentRequest = pm.info.requestName;
    pm.execution.setNextRequest(currentRequest);
} 

```

---

<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:** [May 14, 2024, 2:52pm UTC](https://community.postman.com/t/how-can-i-add-the-loop/60976/10 "2024-05-14T14:52:11Z")

</div>

> [@docking-module-geol7](#):
>
> I am not using the collection runner as once its working, I am going to use Newman CLI.

To test that its working, you will either need to use the collection runner, or do your testing in Newman.

If you run the request direct in Postman, then it will only ever run once.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [May 14, 2024, 2:54pm UTC](https://community.postman.com/t/how-can-i-add-the-loop/60976/11 "2024-05-14T14:54:52Z")

</div>

> [@michaelderekjones](#):
>
> `pm.environment.clear("currentCount");`

Wouldn’t that need to be `unset()`, `clear()` doesn’t take an arg and would remove all variables from the environment.

---

<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:** [May 14, 2024, 3:02pm UTC](https://community.postman.com/t/how-can-i-add-the-loop/60976/12 "2024-05-14T15:02:46Z")

</div>

You are correct, so I’ve edited the post.

---

<div class="post-metadata">

**Author:** ![docking-module-geol7](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@docking-module-geol7](https://community.postman.com/u/docking-module-geol7)\
**Post date:** [May 17, 2024, 7:14pm UTC](https://community.postman.com/t/how-can-i-add-the-loop/60976/13 "2024-05-17T19:14:11Z")

</div>

I was thinking that loop works in postman directly. This worked through Newman.

---

<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:** [May 20, 2024, 7:14pm UTC](https://community.postman.com/t/how-can-i-add-the-loop/60976/14 "2024-05-20T19:14:33Z")

</div>

Closed on May 20, 2024, 7:14pm UTC

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