setNextRequest in loop not executing

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);
    }
}

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;)

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);
    }
}
``

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}}

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.

@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.

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.

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);
} 
2 Likes