# Iterations or Chaining Requests

**URL:** <https://community.postman.com/t/iterations-or-chaining-requests/12498>\
**Category:** Help Hub\
**Created:** [May 8, 2020, 9:44pm UTC](https://community.postman.com/t/iterations-or-chaining-requests/12498 "2020-05-08T21:44:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![matteu1](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@matteu1](https://community.postman.com/u/matteu1)\
**Post date:** [May 8, 2020, 9:44pm UTC](https://community.postman.com/t/iterations-or-chaining-requests/12498/1 "2020-05-08T21:44:10Z")

</div>

I’ve created a variable to extract and store the “partyId” from objects within an array. This works great, and I have about 1,500 partyId’s stored within the variable.

> let PartyList = [1]  
> \_.each(pm.response.json(), (arrItem) =\> {  
> PartyList.push(arrItem.partyId)  
> })
> 
> pm.environment.set(‘PartyList’, JSON.stringify(PartyList))

I have a second GET in my collection I’d like to run based off the results stored in my variable.

//api/common/v1/parties/{{PartyList}}?properties=Relationships

The filter on the endpoint can only support one partyId at once and therefore when I simply insert my variable the request fails. Some how, I need to loop requests while cycling through the values store in my variable.

Hoping there’s a sensible way to approach this. Thank you in advance for any help.

---

<div class="post-metadata">

**Author:** ![kevin.swiber](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kevin.swiber/32/3820_2.png) [@kevin.swiber](https://community.postman.com/u/kevin.swiber)\
**Post date:** [May 8, 2020, 11:03pm UTC](https://community.postman.com/t/iterations-or-chaining-requests/12498/2 "2020-05-08T23:03:19Z")

</div>

Hey @matteu1!

You can use [`postman.setNextRequest`](https://learning.postman.com/docs/postman/scripts/branching-and-looping/) to help with this.

Here’s a blog post explaining how this is done: [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/)

And here’s a template you can use to try it out: [https://explore.postman.com/templates/677/dynamicloop](https://explore.postman.com/templates/677/dynamicloop)

Take a look at the pre-request and test scripts in that example template.

You can adjust variables to set the full list and the current index. Then you can use `postman.setNextRequest("<name>")` to continue looping. At the end of the loop, you can `postman.setNextRequest(null)`. When you run this in the collection runner, you should see it loop.

Hope this helps.

Best,

Kevin

---

<div class="post-metadata">

**Author:** ![tanviherbals](https://avatars.discourse-cdn.com/v4/letter/t/ecae2f/32.png) [@tanviherbals](https://community.postman.com/u/tanviherbals)\
**Post date:** [May 13, 2020, 7:37am UTC](https://community.postman.com/t/iterations-or-chaining-requests/12498/3 "2020-05-13T07:37:28Z")

</div>

Hey Kevin,

I want to extract the orderId from the GET results and use it in a POST method.

Result from the GET function is as below

{  
“message”: “20 transactions fetched”,  
“transactions”: [  
{  
“settlementStatus”: “NA”,  
“paymentMode”: “UPI”,  
“txStatus”: “SUCCESS”,  
“txTime”: “2020-05-13 09:26:19”,  
“customerEmail”: “[divyavdprabhu@gmail.com](mailto:divyavdprabhu@gmail.com)”,  
“customerPhone”: “9702080035”,  
“customerName”: “Divya Prabhu”,  
“orderNote”: “N/A”,  
“orderAmount”: “780.00”,  
“orderId”: “145023161”,  
“id”: “128499598”,  
“referenceId”: “128499598”,  
“currency”: “INR”,  
“txAmount”: “780.00”,  
“refundStatus”: “NA”  
},  
{  
“settlementStatus”: “NA”,  
“paymentMode”: “NET\_BANKING”,  
“txStatus”: “SUCCESS”,  
“txTime”: “2020-05-12 22:51:19”,  
“customerEmail”: “[rrdalvi278@gmail.com](mailto:rrdalvi278@gmail.com)”,  
“customerPhone”: “7304621164”,  
“customerName”: “Rohit Dalvi”,  
“orderNote”: “N/A”,  
“orderAmount”: “350.00”,  
“orderId”: “145023126”,  
“id”: “128391298”,  
“referenceId”: “128391298”,  
“currency”: “INR”,  
“txAmount”: “350.00”,  
“refundStatus”: “NA”  
},  
{  
“settlementStatus”: “NA”,  
“paymentMode”: “CREDIT\_CARD”,  
“txStatus”: “SUCCESS”,  
“txTime”: “2020-05-11 12:36:11”,  
“customerEmail”: “[VISHWANATH.SAWANT69@YAHOO.COM](mailto:VISHWANATH.SAWANT69@YAHOO.COM)”,  
“customerPhone”: “09322236342”,  
“customerName”: “VISHWANATH SAWANT”,  
“orderNote”: “N/A”,  
“orderAmount”: “750.00”,  
“orderId”: “145022771”,  
“id”: “127635278”,  
“referenceId”: “127635278”,  
“currency”: “INR”,  
“txAmount”: “750.00”,  
“refundStatus”: “NA”  
}  
],  
“lastId”: “127635278”,  
“status”: “OK”  
}

Can you help me with the code to extract the orderId and post them in a loop.

---

<div class="post-metadata">

**Author:** ![tanviherbals](https://avatars.discourse-cdn.com/v4/letter/t/ecae2f/32.png) [@tanviherbals](https://community.postman.com/u/tanviherbals)\
**Post date:** [May 13, 2020, 7:43am UTC](https://community.postman.com/t/iterations-or-chaining-requests/12498/4 "2020-05-13T07:43:46Z")

</div>

I get some array of data using GET function. I want to extract all the orderId from the GET function output and want to use it in a POST function. Is this possible?

{  
“message”: “20 transactions fetched”,  
“transactions”: [  
{  
“orderNote”: “N/A”,  
“orderAmount”: “350.00”,  
“orderId”: “145023126”,

```
    },
    {
        "orderNote": "N/A",
        "orderAmount": "350.00",
        "orderId": "145023126",
        
    },{
        "orderNote": "N/A",
        "orderAmount": "350.00",
        "orderId": "145022771",
    }
],
"lastId": "127635278",
"status": "OK"

```

}

TIA. 🙂

---

<div class="post-metadata">

**Author:** ![matteu1](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@matteu1](https://community.postman.com/u/matteu1)\
**Post date:** [May 15, 2020, 7:41pm UTC](https://community.postman.com/t/iterations-or-chaining-requests/12498/5 "2020-05-15T19:41:44Z")

</div>

> [@matteu1](#):
>
> let PartyList = [1]  
> \_.each(pm.response.json(), (arrItem) =\> {  
> PartyList.push(arrItem.partyId)  
> })
> 
> pm.environment.set(‘PartyList’, JSON.stringify(PartyList))

If you update my code snippet then you’ll be able to extract orderId. Then just create a POST request using the variable you declared.

---

<div class="post-metadata">

**Author:** ![jechellea](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jechellea/32/6905_2.png) [@jechellea](https://community.postman.com/u/jechellea)\
**Post date:** [January 20, 2021, 8:18am UTC](https://community.postman.com/t/iterations-or-chaining-requests/12498/6 "2021-01-20T08:18:45Z")

</div>

I was trying this out but I’m getting an error at the 2nd request run. I can’t tell what causes the error. Can you please help me?

 ![Screen Shot 2021-01-20 at 4.16.54 PM](https://us1.discourse-cdn.com/getpostman/original/3X/e/3/e3ad7b34af92128ffb89e1a93c850a10246b2716.png)

---

<div class="post-metadata">

**Author:** ![jency.stella19](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jency.stella19/32/16633_2.png) [@jency.stella19](https://community.postman.com/u/jency.stella19)\
**Post date:** [January 20, 2021, 8:51am UTC](https://community.postman.com/t/iterations-or-chaining-requests/12498/7 "2021-01-20T08:51:40Z")

</div>

Hi @jechellea seems like semicolon` ;` is missing in the code snippet. Could you please addin the semicolon after the pm.variables and postman.setNextRequest statements in fi and else part.

---

<div class="post-metadata">

**Author:** ![jechellea](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jechellea/32/6905_2.png) [@jechellea](https://community.postman.com/u/jechellea)\
**Post date:** [January 20, 2021, 10:11am UTC](https://community.postman.com/t/iterations-or-chaining-requests/12498/8 "2021-01-20T10:11:10Z")

</div>

Was able to resolve the previous error. I have a question tho. I have multiple requests needed to be performed, is it possible in this kind of approach? In the given example it only loop through 1 request and I have more.

---

<div class="post-metadata">

**Author:** ![arlem](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/arlem/32/3078_2.png) [@arlem](https://community.postman.com/u/arlem)\
**Post date:** [January 20, 2021, 10:17am UTC](https://community.postman.com/t/iterations-or-chaining-requests/12498/9 "2021-01-20T10:17:18Z")

</div>

@jechellea Sure, you can use `postman.setNextRequest("request_name");` in the pre-request scripts and redirect to different requests every time, for example if you want to loop over 3 requests:  
`postman.setNextRequest("request_A");`  
`postman.setNextRequest("request_B");`  
`postman.setNextRequest("request_C");`  
`postman.setNextRequest("request_A");`

Just make sure you have a condition that will stop the flow eventually (using `postman.setNextRequest(null);`) or you’ll end up doing an infinite loop.

You’ll find more info in the Learning Center too: [https://learning.postman.com/docs/running-collections/building-workflows/](https://learning.postman.com/docs/running-collections/building-workflows/)

---

<div class="post-metadata">

**Author:** ![jechellea](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jechellea/32/6905_2.png) [@jechellea](https://community.postman.com/u/jechellea)\
**Post date:** [January 20, 2021, 10:23am UTC](https://community.postman.com/t/iterations-or-chaining-requests/12498/10 "2021-01-20T10:23:32Z")

</div>

Yay! Great. Thanks so much 😊
