Iterations or Chaining Requests

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.

Hey @matteu1!

You can use postman.setNextRequest 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/

And here’s a template you can use to try it out: 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

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”,
“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”,
“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”,
“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.

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. :slight_smile:

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.

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?

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.

1 Like

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.

@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/

2 Likes

Yay! Great. Thanks so much :blush: