# Is there a possibility to add iterations in Monitor

**URL:** <https://community.postman.com/t/is-there-a-possibility-to-add-iterations-in-monitor/18137>\
**Category:** Help Hub\
**Created:** [December 1, 2020, 11:18am UTC](https://community.postman.com/t/is-there-a-possibility-to-add-iterations-in-monitor/18137 "2020-12-01T11:18:33Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![jency.stella19](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jency.stella19/32/16633_2.png) [@jency.stella19](https://community.postman.com/u/jency.stella19)\
**Post date:** [December 1, 2020, 11:18am UTC](https://community.postman.com/t/is-there-a-possibility-to-add-iterations-in-monitor/18137/1 "2020-12-01T11:18:33Z")

</div>

I have a collection that has 4 folders in it, each folder needs a different set of iteration to be set. is there a possibility to set it? Kindly help.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [December 1, 2020, 12:24pm UTC](https://community.postman.com/t/is-there-a-possibility-to-add-iterations-in-monitor/18137/2 "2020-12-01T12:24:10Z")

</div>

You can do this in the prerequest and test scripts for each folder. You can initialize a loop counter in the prerequest script and in the test script increment the counter?

What does your layout look like? Do you change the requests in each folder regularly?

---

<div class="post-metadata">

**Author:** ![jency.stella19](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jency.stella19/32/16633_2.png) [@jency.stella19](https://community.postman.com/u/jency.stella19)\
**Post date:** [December 2, 2020, 7:12am UTC](https://community.postman.com/t/is-there-a-possibility-to-add-iterations-in-monitor/18137/3 "2020-12-02T07:12:47Z")

</div>

Hi Allen, ![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/9/a90062e938bb9640b384fe3c08b008abde219dca.png)

this is my layout, in this I need Step 1 and Step 2 to be triggered for 1 iteration and Step 3 to be triggered for 10 iterations.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [December 3, 2020, 12:39pm UTC](https://community.postman.com/t/is-there-a-possibility-to-add-iterations-in-monitor/18137/4 "2020-12-03T12:39:55Z")

</div>

You can add a prerequest script to Step 3 to be a loop counter:

```auto
let counter = pm.variables.get('loopCounter');
if(!counter){
  counter = 0;
}

pm.variables.set('loopCounter', counter++);

```

In your test scripts of the **Step 3** request you can add

```auto
const counter = pm.variables.get('loopCounter');
if(counter < 10) {
   postman.setNextRequest(request.name);
}

```

This will cause just that request to loop 10 times.

It’s the same problem/solution we discussed [in this thread](https://community.postman.com/t/there-was-an-error-in-evaluating-the-pre-request-script-typeerror-cannot-read-property-shift-of-undefined/18094/13)

---

<div class="post-metadata">

**Author:** ![jency.stella19](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jency.stella19/32/16633_2.png) [@jency.stella19](https://community.postman.com/u/jency.stella19)\
**Post date:** [December 4, 2020, 7:24am UTC](https://community.postman.com/t/is-there-a-possibility-to-add-iterations-in-monitor/18137/5 "2020-12-04T07:24:46Z")

</div>

Hi Allen thanks for the reply, yes this will loop in through but after 10 iterations I need to exit.

I tried to put postman.setNextRequest(null); in the prerequest by setting condition as:  
// //exit from pgm

let counter\_end = parseInt(pm.environment.get(“counter”));

if (counter\_end == 10)

{  
postman.setNextRequest(null);

};  
but that’s not working;

---

<div class="post-metadata">

**Author:** ![jency.stella19](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jency.stella19/32/16633_2.png) [@jency.stella19](https://community.postman.com/u/jency.stella19)\
**Post date:** [December 4, 2020, 8:54am UTC](https://community.postman.com/t/is-there-a-possibility-to-add-iterations-in-monitor/18137/6 "2020-12-04T08:54:59Z")

</div>

On further checking how to stop execution using a command in the Pre-request, got to see the post [https://github.com/postmanlabs/postman-app-support/issues/8929](https://github.com/postmanlabs/postman-app-support/issues/8929)

which is what I wanted, this is in-progess if I’m not wrong based on the commnets

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [December 4, 2020, 12:24pm UTC](https://community.postman.com/t/is-there-a-possibility-to-add-iterations-in-monitor/18137/7 "2020-12-04T12:24:52Z")

</div>

if Step 3 is the last request in your collection, the monitor should just stop after that 10th iteration. Can you show me a screenshot of what you have in the prerequest and test script tab?

---

<div class="post-metadata">

**Author:** ![jency.stella19](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jency.stella19/32/16633_2.png) [@jency.stella19](https://community.postman.com/u/jency.stella19)\
**Post date:** [December 7, 2020, 10:14am UTC](https://community.postman.com/t/is-there-a-possibility-to-add-iterations-in-monitor/18137/8 "2020-12-07T10:14:52Z")

</div>

Hi @allenheltondev

In the pre-request I have this

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/d/4da0ecbd688813b7337b6fbe5b49849103fc66a2.png)

In the Test I have this

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

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [December 7, 2020, 12:31pm UTC](https://community.postman.com/t/is-there-a-possibility-to-add-iterations-in-monitor/18137/9 "2020-12-07T12:31:19Z")

</div>

You’re pulling from local variables in your test scripts. You should be pulling counter out of the environment since that’s where you’re setting it in the prerequest script.

---

<div class="post-metadata">

**Author:** ![jency.stella19](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jency.stella19/32/16633_2.png) [@jency.stella19](https://community.postman.com/u/jency.stella19)\
**Post date:** [December 8, 2020, 7:27am UTC](https://community.postman.com/t/is-there-a-possibility-to-add-iterations-in-monitor/18137/10 "2020-12-08T07:27:22Z")

</div>

That was my bad Allen I pasted the wrong part, here is the correct script below:

Code:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/5/e/5eb9610e5129e1ffe32676549f8a8920ad2bb91a.png)

Still pgm is not exiting, after counter 10 its incrementing to 11 and since my logic after that is not valid it hits error at 11

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

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [December 8, 2020, 12:20pm UTC](https://community.postman.com/t/is-there-a-possibility-to-add-iterations-in-monitor/18137/11 "2020-12-08T12:20:22Z")

</div>

You need to make the if condition only less than, not less than or equal to. Since the prerequest script is the one that is incrementing the counter, your test scripts are going to “be one ahead”. So your problem should resolved if you change your script to:

```auto
const counter = pm.environment.get('counter');
const numberOfLoops = 10;
if(counter < numberOfLoops) {
  postman.setNextRequest('Testing-UCI Check - Baed on latest 10 Entity fetched from Step3');
}

```

---

<div class="post-metadata">

**Author:** ![jency.stella19](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jency.stella19/32/16633_2.png) [@jency.stella19](https://community.postman.com/u/jency.stella19)\
**Post date:** [December 9, 2020, 3:16am UTC](https://community.postman.com/t/is-there-a-possibility-to-add-iterations-in-monitor/18137/12 "2020-12-09T03:16:52Z")

</div>

Hi Allen, thank you so much it worked, that was a great help indeed 🙂

---

<div class="post-metadata">

**Author:** ![jency.stella19](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jency.stella19/32/16633_2.png) [@jency.stella19](https://community.postman.com/u/jency.stella19)\
**Post date:** [January 20, 2021, 10:54am UTC](https://community.postman.com/t/is-there-a-possibility-to-add-iterations-in-monitor/18137/13 "2021-01-20T10:54:30Z")

</div>

Hi @allenheltondev need your help in this same topic again, the above same logic when executed in newman is going in infinte loop not sure why is it so, could you please help on this.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [January 20, 2021, 11:45am UTC](https://community.postman.com/t/is-there-a-possibility-to-add-iterations-in-monitor/18137/14 "2021-01-20T11:45:04Z")

</div>

That one has me puzzled, it shouldn’t matter where it’s being executed from. It possibly has to do with the environment variable.

You could try to switch the `counter` environment variable to a collection variable instead.

So anywhere you see `pm.environment.get('counter')` or `pm.environment.set('counter', value)`  
replace it with `pm.collectionVariables.get('counter')` or `pm.collectionVariables.set('counter' value)`

---

<div class="post-metadata">

**Author:** ![jency.stella19](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jency.stella19/32/16633_2.png) [@jency.stella19](https://community.postman.com/u/jency.stella19)\
**Post date:** [January 21, 2021, 11:24am UTC](https://community.postman.com/t/is-there-a-possibility-to-add-iterations-in-monitor/18137/15 "2021-01-21T11:24:26Z")

</div>

Hi @allenheltondev thanks for the help that worked 🙂

But I’m curious why the environment variables are not getting reflected during runtime ☹

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [January 21, 2021, 11:58am UTC](https://community.postman.com/t/is-there-a-possibility-to-add-iterations-in-monitor/18137/16 "2021-01-21T11:58:54Z")

</div>

My thought process behind the solution was:

“If you aren’t explicitly passing in an environment to newman, there are no environment variables to manipulate”

---

<div class="post-metadata">

**Author:** ![jency.stella19](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jency.stella19/32/16633_2.png) [@jency.stella19](https://community.postman.com/u/jency.stella19)\
**Post date:** [January 22, 2021, 6:33am UTC](https://community.postman.com/t/is-there-a-possibility-to-add-iterations-in-monitor/18137/17 "2021-01-22T06:33:56Z")

</div>

Hi @allenheltondev I did pass the environment file while executing in newman

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [January 22, 2021, 11:46am UTC](https://community.postman.com/t/is-there-a-possibility-to-add-iterations-in-monitor/18137/18 "2021-01-22T11:46:14Z")

</div>

Then I’m not sure. Glad it works now though.
