setNextRequest() doesn't work

I have the below tests shown in the ‘with default values’ folder. The first test is an initializer. The next 4 tests are unique and last two are common. Each unique test should call the common requests one after another(eg. ‘WC valid request with all default values’ calls Bump shipment which will then call Get Booked Status). So, I have set setNextRequest() in all 4 unique ‘tests’ to call common requests as mentioned previously.
I have the below in my initializer Tests:-

But when I run the folder through collection runner, it just runs the initializer, but not the rest of 4 unique tests:-

Can anyone suggest a solution for this?

Hey @oasis,

Not sure I fully understand what you’re trying to achieve here, with the way that you have this. Is the intention that it’s going to run the first request and then hit 5 other requests?

If it is, that’s not really how that function works - You should be setting up a workflow that moves between a request and once it’s completed, it moves on the to next specified request in the chain.

You mentioned running the folder in the Collection Runner, do any of those requests in that code live outside of that folder? All the requests mentioned would need to be selected in order for them to be run with that command.

Have you tried a simplified version of what you have to test your logic before trying to everything in one go?

The last case statement should also be setNextRequest(null) and not the way you have it or it would be looking for a request name called null.

@dannydainton, This is what I am trying to achieve:-

WC valid request with all default values —> Bump Shipment --> Get Booked Status
WC valid request with pickup date on day 90 —> Bump Shipment --> Get Booked Status
WC valid request without ‘serviceCode’ —> Bump Shipment --> Get Booked Status
WC invalid request with pickup date>90 days —> Bump Shipment --> Get Booked Status

I do have set the above workflow implemented through ‘Tests’ in each of those above requests.
Eg. In ‘WC valid request with all default values’ I have—>
postman.setNextRequest(“Bump Shipment”);
Then in Bump Shipment I have postman.setNextRequest(“Get booked status”);

All requests reside inside the same folder.
I will correct the ‘null’ one.

Are you trying to run parallel executions of these or multiple workflows at the same time?

As in send the first “dummy” request to start the workflow from the tests - loop through that request array and start each of those workflows, as you described above?

It’s not really parallel as the items will be picked up from array one after another as shown here:-

function myFunction() {
var requests=[
    "WC valid request with all default values",
    "WC valid request with pickup date  on day 90",
    "WC valid request without 'serviceCode'",
    "WC invalid request with pickup date>90 days",

    case "WC valid request with all default values":   
    postman.setNextRequest("WC valid request with all default values");
    case "WC valid request with pickup date  on day 90":
    postman.setNextRequest("WC valid request with pickup date  on day 90");
    case "WC valid request without 'serviceCode'":
    postman.setNextRequest("WC valid request without 'serviceCode'");
    case "WC invalid request with pickup date>90 days":
    postman.setNextRequest("WC invalid request with pickup date>90 days");

    case "Stop":
console.log("Array item not found");

It it going back to this request and picking up the next one in the array though?

I’m confused by that function and the reason why you have that - what’s that doing, is something calling that?

Have you tried it for 1 request from that array and it worked through the flow the way you expected?

Hi @oasis,

If I understand your use-case correctly, the solution you have implemented would not work.

postman.setNextRequest() is not a “call” to the request it just sets which request will be executed, once the “current request” + “test-script” finish executing.

If postman.setNextRequest is called multiple times in a script, the last request set by postman.setNexRequest() will be executed.

In your initializer request, you have a for() loop iterating over request names and based on the current item you are setting the next-request to execute. Since the last request set by the for-loop is null, which means do not execute any other request, only your initializer request runs.

Read-up the documentation for more details:

Potential solution

On a high-level, you would want to store your array and an index in an environment variable.

For example in your initializer request’s Pre-request script do this

var requests = [ 
    "WC invalid request with pickup date>90 days",
    "WC valid request without 'serviceCode'",
    "WC valid request with pickup date  on day 90"

pm.collectionVariables.set('requests', JSON.stringify(requests));

You can see I’ve removed “WC valid request with all default values” from the array, since this request will get executed automatically after the initializer-request completes. And I have reversed the order - since we will be using it as a stack.

Now - in your last request Get booked status - in the test script - at the very end - put the following.

var requests = JSON.parse(pm.collectionVariables.get(`requests`)); // Retrieve the collection variable's value
var nextRequest = request.pop(); // This will be next request's name we want to execute

// Update the collection variable's value
pm.collectionVariables.set('requests', JSON.stringify(requests));

if(nextRequest === 'Stop') { 
} else {

What this will do is, that if the popped item is ‘Stop’, Postman will stop execution - if not, it will use that value to set the next request to run.

1 Like

Hi @amit, That did the trick:) I have only started using Postman recently and came from a Java- RestAssured background… I really appreciate your answer. Thank you so much .
@dannydainton, thanks for your input too.

1 Like