# setNextRequest in loop not executing

**URL:** <https://community.postman.com/t/setnextrequest-in-loop-not-executing/18591>\
**Category:** Help Hub\
**Tags:** collections\
**Created:** [December 20, 2020, 7:12pm UTC](https://community.postman.com/t/setnextrequest-in-loop-not-executing/18591 "2020-12-20T19:12:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahafner](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@ahafner](https://community.postman.com/u/ahafner)\
**Post date:** [December 20, 2020, 7:12pm UTC](https://community.postman.com/t/setnextrequest-in-loop-not-executing/18591/1 "2020-12-20T19:12:08Z")

</div>

I am trying to run the same request multiple times based on a JSON array. The issue I have is that it only executes the request once, like it’s ignoring the setNextRequest. I’ve seen a bunch of posts similar to this issue, but none of the suggestions seem to resolve my issue. I am running it via a Collection runner and it is not reacting as I expect. I have the log statement and it is displaying each iteration (0 and 1), but the “get vehicle” request only occurs once instead of twice as I would expect and it has the property value of “truck” so its only getting the value of the last item in the array. Any suggestions would be appreciated.

Here is my code that is in the Pre-Request Script:

```
let attributeArray = [{
    "cars": '{"type": "string"}'
},
{
    "trucks": '{"type": "string"}'
}
];

for (var i = 0; i < attributeArray.length; i++)
{
    var obj = attributeArray[i];

    for (var property in obj)
    {
        var propertyType = obj[property];

        pm.collectionVariables.set("attributeProperty", property);
        pm.collectionVariables.set("attributePropertyType", propertyType);

        postman.setNextRequest("get vehicle");

        console.log(i);
    }
}
```

---

<div class="post-metadata">

**Author:** ![taehoshino](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/taehoshino/32/6608_2.png) [@taehoshino](https://community.postman.com/u/taehoshino)\
**Post date:** [December 21, 2020, 2:23am UTC](https://community.postman.com/t/setnextrequest-in-loop-not-executing/18591/2 "2020-12-21T02:23:44Z")

</div>

Hi! I can see that you want to run a for loop inside pre-request script. Pre-request script/Tests script are executed before/after the request, i.e.  
Pre-request script → Request execution → Tests script  
So what’s happening here would be:

1. for loop is executed fully in a pre-request script
2. The request is sent  
That’s why only a single request is being sent.

So instead, what you can do is:

1. Set an array as a collection variable in the Tests script of a previous request (e.g. `pm.collectionVariables.set('myArray', myArray)`) or as a initial value of collection variable
2. Retrieve that array value from the collection variable, update it (by using `shift` or `pop`) and then repeat the request, e.g.

> ```
> let myArray = pm.collectionVariables.get('myArray')
> 
> // Get first element from an array and update environmental values
> const elem = myArray.shift()
> pm.collectionVariables.set('myVar', elem)
> pm.collectionVariables.set('myArray', myArray)
> 
> // If elem is not undefined, run the request again, otherwise stop
> if (elem) {
> postman.setNextRequest('GET Request')
> } else {
> postman.setNextRequest(null)
> }
> 
> ```

I hope this helps. Please let me know if you have any further question;)

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [December 21, 2020, 3:44am UTC](https://community.postman.com/t/setnextrequest-in-loop-not-executing/18591/3 "2020-12-21T03:44:14Z")

</div>

```auto
let attributeArray = [{
    "cars": '{"type": "string"}'
},
{
    "trucks": '{"type": "string"}'
}
];

if(pm.info.iteration===0){

pm.environment.set("array",attributeArray)

}
let arr=pm.environment.get("array")

if (arr.length!==0)
{
    var obj = arr.pop()

pm.environment.set("array",arr)
    for (var property in obj)
    {
        var propertyType = obj[property];

        pm.collectionVariables.set("attributeProperty", property);
        pm.collectionVariables.set("attributePropertyType", propertyType);

        postman.setNextRequest("get vehicle");

        console.log(i);
    }
}
``
```

---

<div class="post-metadata">

**Author:** ![waynem](https://avatars.discourse-cdn.com/v4/letter/w/54ee81/32.png) [@waynem](https://community.postman.com/u/waynem)\
**Post date:** [June 11, 2024, 1:46pm UTC](https://community.postman.com/t/setnextrequest-in-loop-not-executing/18591/4 "2024-06-11T13:46:37Z")

</div>

Hi guys

I have 2 requests, the first is a GET which gets an array of items.  
Then i have a post which needs to be called for each of the items in that array.

In the GET request(data source), i have this code in the Post-response section:

> //get mids and put in MIDS var  
> let body = pm.response.json(),  
> MIDS = \_.map(body.result, (result) =\> result.id);  
> console.log(MIDS)
> 
> var numRecords = pm.response.json().totalResults;
> 
> //for each mid call MyPostHttpMethod  
> var i = 0;  
> while(i \< numRecords){  
> console.log(‘Iterating i=’, i)  
> console.log(MIDS[i])  
> pm.collectionVariables.set(‘mid’, MIDS[i]);  
> pm.execution.setNextRequest(‘MyPostHttpMethod’)  
> i = i + 1;  
> }

All looks good but the following method only gets called once:

> setNextRequest(‘MyPostHttpMethod’)

I have no scripts setup in the POST method called by setNextRequest, the POST method is simply called with this url and gets the mid that was set in the loop:  
[http://localhost:8081/v/1/items/{{mid}](http://localhost:8081/v/1/items/%7B%7Bmid%7D)}

I have no idea why it wont call it each time as per the loop, it’s always only once.  
I am using the collection runner with default settings.

Postman details:  
Version 11.1.25  
UI Version: 11.1.25-ui-240604-0453  
Desktop Platform Version: 11.1.14 (11.1.14)

Any ideas would be 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:** [June 11, 2024, 5:06pm UTC](https://community.postman.com/t/setnextrequest-in-loop-not-executing/18591/5 "2024-06-11T17:06:52Z")

</div>

@waynem

This should have been its own question, rather than reviving a three year old thread.

That is not how setNextRequest() works.

All that function does is set the request that will run after the current request and any scripts have completed.

Therefore, it will quickly run through your loop and the last item in your array will be what is finally used for setNextRequest(). Therefore it will only run once.

In your GET request, you need to save your MIDS array to a collection variable which will be used to control the loop.

```auto
let body = pm.response.json();
let MIDS = _.map(body.result, (result) => result.id);
pm.collectionVariables.set("MIDS", MIDS);

```

Then in the pre-request script for the POST request, you will use setNextRequest() in conjunction with a method available to arrays called shift(). Shift() gets the first element in an array, and then deletes it from the array.

Something like.

```auto
if (typeof array === 'undefined' || array.length == 0) {
    array = pm.variables.get("MIDS")
}

let currentMid = array.shift();
pm.collectionVariables.set("mid", currentMid);

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

```
