Chaining Multiple Requests together

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

Hey @mark.sadegursky! Welcome back to our community :wave:

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!