The statement "setNextRequest(null)" is not working

Hi @danny-dainton, I’m currently working on several projects at my company, and right now I need to stop the execution when a variable is empty. However, when I enter pm.execution.setNextRequest(null), it doesn’t work. I remember that a few months ago it worked, but apparently in the current versions it stopped working. Could you help me with another type of statement to stop the execution? I’m attaching evidence. Thank you.

var s = pm.iterationData.get("n1");

// Check if variable 's' is empty

if (!s) {

    console.log("Variable 's' is empty, stopping execution.");

    pm.execution.setNextRequest(null); // Stop the execution of the next request

}

The image shows that it continues executing even when I enter the condition.

Can you please show the requests in your collection, is it a single request?

Your runner appears to show 3 iterations. Therefore three records in your data file. You have 3 matching GET requests in your console logs. This would appear to indicate that you only have one request in your collection that you are looping over with the data file.

setNextRequest() just sets the next request that will run after the current request and any code. It will always run the request that the code is attached to. If you only have a single request in your collection, it will run that once for each line or record in your data file.

If you want to prevent it running the request if the variable is not set, then you need to use the skiprequest() function instead in the pre-request script.

1 Like

Hi @michaelderekjones , yes It’s a single request, and I’m only doing an echo to test the functionality. my file only contains 3 records, what I’m trying to do is make the execution stop when an empty value appears and not continue running. According to the documentation and community posts, setNextRequest(null) should stop the execution. In this case, I should only see 2 or 1 iterations and not 3."

This is the data:

n1,n2,n3,n4,
1,2,3,4
,6,4,,
4,,5,9,

setNextRequest() will only stop the execution within that iteration.

It sets the next request that will run. It does not affect the running of the current request. It can’t stop the first request in your collection from running. That request and all of its code will always run. It sets the request that will run after that request.

The collection runner will run once for each line\iteration in your data file. I think that even if you use the skipRequest() feature and no requests are executed for an iteration, it will still show that iteration as executed.

setNextRequest() only manages the current iteration. If you use setNextRequest(null) it will just move onto the next iteration. What I couldn’t tell is if you wanted this to stop all execution if one of the iterations fail or move onto the next iteration.

Either way, it looks like you should be using skipRequest() not setNextRequest().

1 Like

Hi @michaelderekjones yes, I think after analyzing what you mentioned, I understand how it works. My idea was that when it found an empty variable, it would completely stop the execution, but as you said, it only skips to the next one. It’s not what I expected, but I’ll use either skipRequest() or setNextRequest(null).". Thanks for your support.

It was really helpful reading this thread — I ran into a similar issue and also assumed setNextRequest(null) would completely stop the run, not just move to the next iteration. Your explanation about how Postman handles each data line separately makes the behavior much clearer. It’s a bit like expecting a process to stop at the first empty value, but the runner still completes all rows anyway. Thanks for breaking it down so well; it saved me a lot of confusion while organizing my own workflow (which, funnily enough, I was structuring around a product import feature related to Diamond Necklaces). Appreciate the insights!

2 Likes