setNextRequest not making multiple calls when added in for loop

I am trying to make a chain on calls i.e. 1 -> 2 -> 3

The 2nd call is responsible to provide an array. I am reading the array response, storing it inside environment variable and then using below line to make the 3rd request.


There are 2 values in the collection. Hence, I assume two calls should be done for the 3rd API. But at present, it is only called once. How can I make the above line await for the response to come back and then iterate through the for loop again. At present, in the logs I see only one call, hence the above line is not even calling it multiple times.

Can anyone please let me know, what correction I need to do here …so that 2 calls are made to the 3rd API

Hey @sameer.s.panicker

Would you be able to provide an example of the response body and the exact names of the requests please?

Also, ensure that you’re saving the request if you have modified it, the Collection Runner wouldn’t know about any new changes if you haven’t.

Hey @sameer.s.panicker

Since you are trying to loop through the requests, you don’t need a for loop for that. Instead have a counter and save it as environment variable. Increment the counter and re-save it. Give the below code a try:

// initialize counter as an environment variable, if not already done
if (!pm.environment.has("counter")) {
    console.log("set counter to 0");
    pm.environment.set("counter", 0);
let currentCount = parseInt(pm.environment.get("counter")); 
let numberOfIterations = jsonData.value.length;

if (currentCount < numberOfIterations) {
// here set your environment variables- filechangedurl and filechangedPath
    pm.environment.set("filechangedurl", jsonData.value[currentCount].item.url);
    // increment the counter and re-save the value as an environment variable
    pm.environment.set("counter", currentCount);
    console.log("all set for 3rd request");

Note: Make a request back to your 2nd request in 3rd request. Also set the next actions according to your needs like setnextrequest to null when certain condition met.
Hope this helps :slightly_smiling_face:

1 Like

Thanks Manpreet !!

Based on your code, I understood the flow or how to use setNextRequest. I made some code changes…added counter as you mentioned and things are working as expected.

Thanks again…

I am glad it helped you!!