setNextRequest won't run

I have a request in Postman that will create a User for me. I want to run it automatically for a certain amount of time to save time preparing datas. I try using setNextRequest but it doesn’t work. I have tried to put it in Pre-request Script, Tests tab of the request itself as well as the Collection but it just won’t run. I have certainly sure that all of my item is saved, none of them has orange dots.

var count = 0;
while(count < 5){
    postman.setNextRequest("CreatePersonWithUser");
    console.log("This is the " + count + " time");
    count++;
}

My console log:

This is the 1 time
 
This is the 2 time
 
This is the 3 time
 
This is the 4 time
 
This is the 5 time
 
POST

Hi @hoangdh1012,

setNextRequest only run within a collection runner. Further more the request you are setting must be in the same collection and sub folder as the request that will execute the set next command. see documentation Building request workflows | Postman Learning Center

1 Like

Thanks. The setNextRequest is working now. But it’s running in an infinite loop instead of following condition of “count”. Could you tell me what’s wrong with it?

Hi @hoangdh1012,

If you using the same code as above. you need to initialize the count variable elsewhere. The code in the test will be executed every time the request runs and your variable will be initialized at 0 every time causing the loop. You should set that variable up as a collection variable. You can retrieve the collection variables from the test script section using pm.collectionVariables.get().

Check out similar post. How do I set a Collection variable?