How to use two setNextRequest under same pre-request-scripts

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Here’s an outline with best practices for making your inquiry.

My question:
How to use two setNextRequest under same pre-request-scripts

if(Array.isArray(configurationconfigCode) && configurationconfigCode.length > 0){

Thanks in Advance

That’s not going to work.

postman.setNextRequest() just sets which request will be executed once the current request and scripts have finished executing. It does not run the request immediately.

In your example the second (and final) setNextRequest will be executed.

You could just put this request, the details request, and the data request in a folder in order and reverse the logic of the IF statement and use setNextRequest(null) to prevent it running the following requests if the array has length = 0.

You can add it to pre-request script of collection

I want to fetch the data from the first API response and then I need to call the second API so i called like above is there any other way .

You can, but that will still only run the second and final request (and after the request and the rest of the scripts have run including what is in the tests tab).

Have you been through the Learning Centre, as it shows basic examples of using work flows.

Why can’t you just have a request that calls the first API, and then save the specific elements you need for the second request to collection\environment variables.

The second request would then just be a normal request next in the folder with variables\placeholders pointing to the collection\environment variables you set in the first request.

If you need to stop processing the folder after the first request (if a particular response is not found), then you can use setNextRequest(null) to stop processing the requests in the folder.

Please remember, setNextRequest only executes with with the Collection Runner.

You can use pm
Send request(null) to stop execution , :partying_face: don’t know if the process have changed

I was getting an array of data from one API then if I passed each value data into a second API it will get some input then the same data needed to use a third API.
Note: Array size is like 298 records

Through collection runner only we are executing but one API call triggering every time not second setNextRequest not calling

The first request I would run normally, and then save the results to a collectionVariable as an array or object. Please remember to stringify the object\array before saving it to the collectionVariable, and JSON.parse it when retrieving.

The second request would have a pre-request scripts that retrieves the previously saved array, and uses array.shift() to return the first value in the array (the functional also removes that value from the array). You can then save both the current value and the updated array back to collectionVariables that can be used in the body of of the request.

In the tests tab for that request. You will retrieve the array again, and keep looping using sendRequest and setNextRequest while the array is larger than zero.

The sendRequest would call your third API, and setNextRequest will control the loop.