pm.execution.setNextRequest(null) just does not work

I’ve been pouring through google and and the documenation for days but I can’t get this to work.

This is my most simple example. In my Post-response block I have only these lines:

console.log(“CALLING pm.execution.setNextRequest(null);”);
pm.execution.setNextRequest(null);

I run my collection and set the iterations to 3, and when it runs it outputs the console message but all 3 iterations execute, even though they should stop.

The official documentation says:

To stop a workflow, add the following code on the Scripts > Post-response tab of a request:
pm.execution.setNextRequest(null);
The collection run will stop after Postman completes the current request.

However it just doesn’t stop.

My actual goal here is to have the collection run 10 times with an variable incrementing from 1 to 10, but if we have a status code that is not 201 I want the collection to stop executing. The example above is a very simplistic one, showing that Postman just isn’t working as per the docs.

Am I missing something? Are collection runs not the right way to do it - should I run the collection once and instead have a for loop in the script or something?

Log snippet from 3 runs:

GET https://community.postman.com/?test=1
CALLING pm.execution.setNextRequest(null);
GET https://community.postman.com/?test=1
CALLING pm.execution.setNextRequest(null);
GET https://community.postman.com/?test=1
CALLING pm.execution.setNextRequest(null);

I’ll attach a screesnhot if I can.

Thanks

setNextRequest(null) just stops the current iteration.

The following iteration will still run.

You could potentially add a collection variable just before you setNextRequest(null) and set the value to “stop” or something similar.

Then in a pre-request script at the collection level, you retrieve this variable, and if the value is stop, also call setNextRequest(null).

Thanks. Have you tried it?

As per your suggestion, I have altered my simple test to

Then in a pre-request script at the collection level (…) also call setNextRequest(null).

Now it’s calling pm.execution.setNextRequest(null); all the time, and still runs 3 times when it probably shouldn’t run at all?

Basically no matter when or how you call executing pm.execution.setNextRequest(null); it just keeps running anyway.

Collection level Pre-Request: executing pm.execution.setNextRequest(null);
GET https://community.postman.com/?test=1
Request Level Post-Response: executing pm.execution.setNextRequest(null);
 
Collection level Pre-Request: executing pm.execution.setNextRequest(null);
GET https://community.postman.com/?test=1
Request Level Post-Response: executing pm.execution.setNextRequest(null);
 
Collection level Pre-Request: executing pm.execution.setNextRequest(null);
GET https://community.postman.com/?test=1
Request Level Post-Response: executing pm.execution.setNextRequest(null);


Full json export:

{
	"info": {
		"_postman_id": "7cfd5a65-f198-4149-bf15-96e852dee506",
		"name": "setNextRequest bug",
		"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
		"_exporter_id": "10569225"
	},
	"item": [
		{
			"name": "New Request",
			"event": [
				{
					"listen": "test",
					"script": {
						"exec": [
							"console.log(\"Request Level Post-Response: executing pm.execution.setNextRequest(null);\");\r",
							"pm.execution.setNextRequest(null);"
						],
						"type": "text/javascript",
						"packages": {}
					}
				},
				{
					"listen": "prerequest",
					"script": {
						"exec": [
							""
						],
						"type": "text/javascript",
						"packages": {}
					}
				}
			],
			"request": {
				"method": "GET",
				"header": [],
				"url": {
					"raw": "https://community.postman.com/?test={{pageNumber}}",
					"protocol": "https",
					"host": [
						"community",
						"postman",
						"com"
					],
					"path": [
						""
					],
					"query": [
						{
							"key": "test",
							"value": "{{pageNumber}}"
						}
					]
				}
			},
			"response": []
		}
	],
	"event": [
		{
			"listen": "prerequest",
			"script": {
				"type": "text/javascript",
				"packages": {},
				"exec": [
					"console.log(\"Collection level Pre-Request: executing pm.execution.setNextRequest(null);\");\r",
					"pm.execution.setNextRequest(null);"
				]
			}
		},
		{
			"listen": "test",
			"script": {
				"type": "text/javascript",
				"packages": {},
				"exec": [
					""
				]
			}
		}
	],
	"variable": [
		{
			"key": "pageNumber",
			"value": "1"
		}
	]
}

Thinking about it, setNextRequest just sets the next request that will run after the current request and any code in the pre or post response scripts.

It will always run the first request in the collection.

If you want to skip that request completely, you will need to use the skip request feature.

https://community.postman.com/t/the-new-skip-request-feature/

Thanks, that seems to work. In my earlier googling, people had tried skipRequest vs setNextRequest with undesired results so I didn’t try it myself, as the documenation alludes to setNextRequest being king here.

All sorted for now - thanks again.

1 Like