Postman Workflows with setNextRequest — Controlling Collection Execution Flow
In this tutorial, you will learn about Postman Workflows with setNextRequest. We cover key concepts, practical examples, and best practices to help you master this topic.
Postman's setNextRequest function controls collection run flow by specifying which request executes next, enabling conditional branching, looping, and dynamic test sequences.
Code Example: Conditional Workflow with setNextRequest
// Tests tab — conditional branching
const jsonData = pm.response.json();
const threatId = jsonData.id;
if (jsonData.severity === "critical") {
// Escalate — run the escalation request
pm.collectionVariables.set("escalatedThreatId", threatId);
postman.setNextRequest("Escalate Critical Threat");
console.log(`Escalating critical threat: ${threatId}`);
} else {
// Normal flow — proceed to list threats
postman.setNextRequest("List All Threats");
}
Code Example: Retry Loop with Maximum Attempts
// Pre-request for async job polling
const maxRetries = 10;
const retryCount = pm.collectionVariables.get("retryCount") || 0;
if (retryCount >= maxRetries) {
console.error("Max retries exceeded");
pm.expect.fail("Job did not complete within expected time");
postman.setNextRequest(null); // Stop the collection
}
pm.collectionVariables.set("retryCount", retryCount + 1);
// Test script for polling
if (pm.response.code === 200 && pm.response.json().status === "completed") {
console.log(`Job completed on attempt ${retryCount + 1}`);
pm.collectionVariables.set("retryCount", 0);
postman.setNextRequest("Verify Job Results");
} else {
// Wait and retry
setTimeout(() => {}, 2000);
postman.setNextRequest("Poll Job Status"); // Loop back
}
Common Mistakes
1. Infinite Loops
Without proper exit conditions, setNextRequest creates infinite loops. Always track iterations and stop at a maximum count.
2. setNextRequest Ignored in Newman
setNextRequest works in both Postman and Newman, but the execution order must be configured correctly. Test workflows in Postman first.
3. Not Resetting the Flow
After a workflow runs, the next normal collection run uses the modified flow. Reset setNextRequest in the collection pre-request script.
What's Next
Now learn about Newman CLI Deep Dive for running Postman collections in CI/CD.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro