# Trying to use .setNextRequest efficiently

**URL:** <https://community.postman.com/t/trying-to-use-setnextrequest-efficiently/7057>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [August 8, 2019, 2:23pm UTC](https://community.postman.com/t/trying-to-use-setnextrequest-efficiently/7057 "2019-08-08T14:23:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![LiamC](https://avatars.discourse-cdn.com/v4/letter/l/7ab992/32.png) [@LiamC](https://community.postman.com/u/LiamC)\
**Post date:** [August 8, 2019, 2:23pm UTC](https://community.postman.com/t/trying-to-use-setnextrequest-efficiently/7057/1 "2019-08-08T14:23:12Z")

</div>

Hi, I’m a very fresh QA Tester currently teaching myself JavaScript, Postman and Jenkins from scratch for work.

I’m fiddling around with a practice API at the moment, trying to write up test cases and get a better workflow going with my requests.  
I’m basically trying to achieve: Get List of Games\>Post a Game\>Get List of Games\>Delete added Game\>Get List of Games.

However, rather that duplicate the GET request, I wanted to use .setNextRequest and conditional logic to achieve this workflow with just the 1 GET request.  
I’m sure there’s probably a much smarter way of doing what I’m trying to attempt. It’s my 1st time trying out this kind of thing. Essentially I figured if I had a counter, I could use that to create conditionals that would direct my requests the way I want. I’m having difficulty understanding where I should define variables because they’re in separate windows.

_The Request names are just placeholders._  
Code below–

**On the GET request Tests Tab:**  
pm.globals.set(“numberOfGames”, 1);  
pm.globals.set(“currentGameCount”, 0);  
if (gameCount === pm.globals.get(numberOfGames)) {  
postman.setNextRequest(“DEL”);  
} else {  
postman.setNextRequest(“POST”);  
}

**On the POST request Tests tab:**  
let gameCount = pm.globals.get(“currentGameCount”);  
gameCount++;  
pm.globals.set(“currentGameCount”, gameCount);

if (gameCount \< pm.globals.get(“numberOfGames”)) {  
postman.setNextRequest(“POST”);  
} else {  
postman.setNextRequest(“GET”);  
}

On the DEL request Tests tab:  
if (pm.response.to.have.status(204)) {  
postman.setNextRequest(“DEL”);  
} else {  
postman.setNextRequest(“POST”);  
}

---

<div class="post-metadata">

**Author:** ![vdespa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vdespa/32/2770_2.png) [@vdespa](https://community.postman.com/u/vdespa)\
**Post date:** [August 8, 2019, 5:11pm UTC](https://community.postman.com/t/trying-to-use-setnextrequest-efficiently/7057/2 "2019-08-08T17:11:42Z")

</div>

I would do it more or less like this:

- the post and delete will always return to get, so use postman.setNextRequest(“Get”);
- the get request needs to figure out where to go next, post or delete. You can make the decision by evaluating a variable

Here is a sample collection exemplifying this:

[https://www.getpostman.com/collections/8f80d004a0574c62ecce](https://www.getpostman.com/collections/8f80d004a0574c62ecce)

It will stop after 3 iterations. I hope it helps.

---

<div class="post-metadata">

**Author:** ![jetison](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jetison/32/12_2.png) [@jetison](https://community.postman.com/u/jetison)\
**Post date:** [August 8, 2019, 5:15pm UTC](https://community.postman.com/t/trying-to-use-setnextrequest-efficiently/7057/3 "2019-08-08T17:15:37Z")

</div>

Hi there @LiamC -

As you said, there’s a bunch of ways you can do this kind of stuff. I would take a look at 1 of these [templates](https://learning.getpostman.com/docs/postman/launching_postman/newbutton/#templates) that walk through how to use `postman.setNextRequest()`. You can import the template to play around with the examples. Or read through the written tutorials.

Both examples show you how to `get` and `set` your counter or state using environment and local variables respectively.

**Link Checker** - [https://blog.getpostman.com/2017/06/23/check-for-broken-links-on-your-website-using-a-postman-collection/](https://blog.getpostman.com/2017/06/23/check-for-broken-links-on-your-website-using-a-postman-collection/)  
**DynamicLoop** - [https://ambertests.com/2019/01/28/postman-how-to-dynamic-iteration-within-a-collection/](https://ambertests.com/2019/01/28/postman-how-to-dynamic-iteration-within-a-collection/)

---

<div class="post-metadata">

**Author:** ![LiamC](https://avatars.discourse-cdn.com/v4/letter/l/7ab992/32.png) [@LiamC](https://community.postman.com/u/LiamC)\
**Post date:** [August 9, 2019, 8:10am UTC](https://community.postman.com/t/trying-to-use-setnextrequest-efficiently/7057/4 "2019-08-09T08:10:59Z")

</div>

Thank you so much for the help!

A few things have just clicked in my head, so I get it now! 🙂

---

<div class="post-metadata">

**Author:** ![jonsworkalias](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jonsworkalias/32/3153_2.png) [@jonsworkalias](https://community.postman.com/u/jonsworkalias)\
**Post date:** [October 24, 2019, 6:25pm UTC](https://community.postman.com/t/trying-to-use-setnextrequest-efficiently/7057/5 "2019-10-24T18:25:42Z")

</div>

> [@vdespa](#):
>
> Here is a sample collection exemplifying this:
> 
> [https://www.getpostman.com/collections/8f80d004a0574c62ecce](https://www.getpostman.com/collections/8f80d004a0574c62ecce)
> 
> It will stop after 3 iterations. I hope it helps.

I’m trying to get the simple setNextRequest function to work - and it seems to never call the next request. I tried your sample, in a new blank workspace, and when I call the Get - it just runs that one step.

Is there a setting or something I need to do to enable this call to be triggered? I’m scratching my head, seems like I am missing something simple but I don’t see it. (I tried my own simple test of 2 blank calls where all that is done is a console log statement and a setNextRequest call - calls in same folder, calls both one word names, lower case, set value to name of second call - just doesn’t trigger the second call…)

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/8/48addf26c6b85bf82fb534aa23c9e227a599c2ce.jpeg)

---

<div class="post-metadata">

**Author:** ![vdespa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vdespa/32/2770_2.png) [@vdespa](https://community.postman.com/u/vdespa)\
**Post date:** [October 25, 2019, 10:59am UTC](https://community.postman.com/t/trying-to-use-setnextrequest-efficiently/7057/6 "2019-10-25T10:59:29Z")

</div>

@jonsworkalias I forgot to specify this but you do need to run the collection in an automatic way, by using the Collection runner or Newman. Otherwise, it does not work.

---

<div class="post-metadata">

**Author:** ![jonsworkalias](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jonsworkalias/32/3153_2.png) [@jonsworkalias](https://community.postman.com/u/jonsworkalias)\
**Post date:** [October 25, 2019, 2:03pm UTC](https://community.postman.com/t/trying-to-use-setnextrequest-efficiently/7057/7 "2019-10-25T14:03:06Z")

</div>

@vdespa Thanks, that would explain it!

I’ve found your video tutorials very helpful, thanks for putting them out there.
