# Chaining Multiple Requests together

**URL:** <https://community.postman.com/t/chaining-multiple-requests-together/19306>\
**Category:** Help Hub\
**Tags:** tests, collections, setnextrequest, request\
**Created:** [January 19, 2021, 6:06pm UTC](https://community.postman.com/t/chaining-multiple-requests-together/19306 "2021-01-19T18:06:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark.sadegursky](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/mark.sadegursky/32/223_2.png) [@mark.sadegursky](https://community.postman.com/u/mark.sadegursky)\
**Post date:** [January 19, 2021, 6:06pm UTC](https://community.postman.com/t/chaining-multiple-requests-together/19306/1 "2021-01-19T18:06:23Z")

</div>

Hi  
Is it possible to chain multiple requests?

Loop 1 → Get List of Channels  
Loop on results from Loop 1  
Call Get List of Channels Messages for each ID in Loop 1  
Loop on results from Loop 2  
call Delete Channels Message for each Message in Loop 2

I can’t figure out a way to loop back to results in Loop 1

Thanks,  
Mark

---

<div class="post-metadata">

**Author:** ![taehoshino](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/taehoshino/32/6608_2.png) [@taehoshino](https://community.postman.com/u/taehoshino)\
**Post date:** [January 20, 2021, 12:46am UTC](https://community.postman.com/t/chaining-multiple-requests-together/19306/2 "2021-01-20T00:46:13Z")

</div>

Hey @mark.sadegursky! Welcome back to our community 👋

You can achieve the chaining of requests by utilizing `postman.setNextRequest` method and environmental variables.

Based on your description of the problem, I have included a pseudo-script in the below for you to get some ideas on how to implement chaining. Including something like below in Tests, you can chain multiple requests.

**Get List of Channels**

```
const channelList = pm.response.json() etc // store channel IDs ['1', '2', '3']
const channelId = channelList.shift() // remove first item from the array

// Set environment variables 
pm.environment.set('channelList', channelList)
pm.environment.set('channelId', channelId)

// Set the next request
postman.setNextRequest('Get List of Channel Messages')

```

**Get List of Channel Messages** - In this request body/params etc, I am assuming you will use channel ID, which you can access by {{channelId}}

```
const messageList = pm.response.json() etc // I am assuming this is an array of message ID, which you will use in the next request. 
const messageId = messageList.shift()

// Set environment variables 
pm.environment.set('messageList', messageList)
pm.environment.set('messageId', messageId)

// Set next request
postman.setNextRequest('Delete Channel Message')

```

**Delete Channel Message** - In this request body/params etc, I am assuming you will use message ID, which you can access by {{messageId}}

```
const messageList = pm.environment.get('messageList')
const messageId = messageList.shift()

if (!messageId) {
    postman.setNextRequest('Delete Channel Message') // Repeat this request while messageList is not empty
} else { // Update environmental variables and move onto the next channel ID
    const channelList = pm.environment.get('channelList')
    const channelId = channelList.shift()
     pm.environment.set('channelList', channelList)
     pm.environment.set('channelId', channelId)
     postman.setNextRequest('Get List of Channel Messages') //
}

```

Hope this helps!

---

<div class="post-metadata">

**Author:** ![wwave](https://avatars.discourse-cdn.com/v4/letter/w/f4b2a3/32.png) [@wwave](https://community.postman.com/u/wwave)\
**Post date:** [May 17, 2022, 1:25pm UTC](https://community.postman.com/t/chaining-multiple-requests-together/19306/3 "2022-05-17T13:25:02Z")

</div>

Hi @taehoshino taehoshino

I think I have the same issue that was asked on this I am trying to get a list of data to scroll through as such till it ends and add them all as collection environment to get put somewhere else the data can be 50 items long sometime others it might only be 2 or 10 etc. I think what you explained above is what I am chasing but I can’t seem to get it to work. How do you get the list of all the data to get added as separate variables ready to get used for the post? I can one get one to get added as a variable at a time I dont understand how to step through the data adding it as separate variables. I have come a long way but I have way more to learn the data is sort of like this. The objective is to add the data till it ends as different variables. Then do it the other way to collect it for a put. Any help would be much appreciated

This is the part of the get

“detail”: [  
{  
“master\_id”: 7549,  
“detail!quantity”: 1,  
“detail!invtext”: “item1”,  
“detail!invamountItem”: 50,  
“detail!nominalcode”: “4-1012”,  
“detail!nominaldesc”: “Visual Equip Rental Income”,  
“detail!Taxlineitem”: 5,  
“detail!invamount”: “50.00”  
},  
{  
“master\_id”: 7549,  
“detail!quantity”: 1,  
“detail!invtext”: “item2”,  
“detail!invamountItem”: 1,  
“detail!nominalcode”: “4-1011”,  
“detail!nominaldesc”: “Audio Equip Rental Income”,  
“detail!Taxlineitem”: 0.1,  
“detail!invamount”: “1.00”  
},  
{  
“master\_id”: 7549,  
“detail!quantity”: 4,  
“detail!invtext”: “item2”,  
“detail!invamountItem”: 2,  
“detail!nominalcode”: “4-1013”,  
“detail!nominaldesc”: “other Income”,  
“detail!Taxlineitem”: 0.2,  
“detail!invamount”: “2.00”  
},  
{  
“master\_id”: 7549,  
“detail!quantity”: 6,  
“detail!invtext”: “item3”,  
“detail!invamountItem”: 2,  
“detail!nominalcode”: “4-1013”,  
“detail!nominaldesc”: “freight Income”,  
“detail!Taxlineitem”: 0.2,  
“detail!invamount”: “2.00”  
},  
{  
“master\_id”: 7549,  
“detail!quantity”: 1,  
“detail!invtext”: “item4”,  
“detail!invamountItem”: 1,  
“detail!nominalcode”: “4-1015”,  
“detail!nominaldesc”: “shop Income”,  
“detail!Taxlineitem”: 0.1,  
“detail!invamount”: “1.00”  
}  
]  
}

This is the Post file example is

```
"Lines": [
	{
		"Type": "Transaction",
		"Description": "1 x item1",
		"Account": {
			"UID": "4-1012"
		},
		"Total": 50.00,
		"Job": null,
		"TaxCode": {
			"UID": "56dd406b-0dcc-4110-bedf-b8a7f778450f"
		}
	},
	{
		"Type": "Transaction",
		"Description": "4 x item2",
		"Account": {
			"UID": "56dd406b-0dcc-4110-bedf-b8a7f778450f"
		},
		"Total": 1.00,
		"Job": null,
		"TaxCode": {
			"UID": "56dd406b-0dcc-4110-bedf-b8a7f778450f"
		}
	},
	{
		"Type": "Transaction",
		"Description": "6 x item3",
		"Account": {
			"UID": "4-1013"
		},
		"Total": -1200256,
		"Job": null,
		"TaxCode": {
			"UID": "56dd406b-0dcc-4110-bedf-b8a7f778450f"
		}
	},
	{
		"Type": "Transaction",
		"Description": "1x item4",
		"Account": {
			"UID": "4-1015"
		},
		"Total": 1.00,
		"Job": null,
		"TaxCode": {
			"UID": "56dd406b-0dcc-4110-bedf-b8a7f778450f"
		}
	}
],
```

---

<div class="post-metadata">

**Author:** ![robminardi](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/robminardi/32/5857_2.png) [@robminardi](https://community.postman.com/u/robminardi)\
**Post date:** [February 6, 2023, 11:26am UTC](https://community.postman.com/t/chaining-multiple-requests-together/19306/4 "2023-02-06T11:26:44Z")

</div>

The way we handle this, is to use the javascript .map function to create an array of values from the response and store them as a variable. Then, we can loop through those values at a later time.

```auto
if(responseCode.code === 200)
{

//Creates an array of objects with the status id and the status name from the response
 let statuses = response.data.statusItem.map(si => {return {ID: si.ID, name: si.name}});
//sorts the above array by status id ascending
 statuses.sort((a,b) => {
     return a.ID - b.ID;
 });
//creates the environment statusArray variable and puts the array in it
 pm.environment.set("statusArray", JSON.stringify(statuses));
}

```

statusArray variable  
[{“ID”:1,“name”:“New Rx”},{“ID”:2,“name”:“Remote Rx”},{“ID”:3,“name”:“Re-Calc”,{“ID”:52,“name”:“Out Hold”}]
