# Chaining Responses and Visualizer with Runner

**URL:** <https://community.postman.com/t/chaining-responses-and-visualizer-with-runner/50194>\
**Category:** Help Hub\
**Tags:** variables, runner, visualizer\
**Created:** [August 2, 2023, 2:12pm UTC](https://community.postman.com/t/chaining-responses-and-visualizer-with-runner/50194 "2023-08-02T14:12:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alcsmith](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/alcsmith/32/16489_2.png) [@alcsmith](https://community.postman.com/u/alcsmith)\
**Post date:** [August 2, 2023, 2:12pm UTC](https://community.postman.com/t/chaining-responses-and-visualizer-with-runner/50194/1 "2023-08-02T14:12:47Z")

</div>

Hi folks -  
Looking to get some assistance and I’m not sure if this is possible or not. I have ever 1000 devices that I need to query via API and get a specific result that would be different for each device.  
I have 2 collections

Collection 1: Does a discovery of the nodes and gives me the following info:  
Name, Group, ID  
----------being snip----------------

```auto
[
    {
        "copyId": "1294490049_3435e002f78568bc_0",
        "groupName": "CG_FOO_BURGER",
        "id": "LONG_FOO_NUMBER",
        "name": "FOO_CHEESE",
        "protectFutureNewVmdks": true,
        "replicaVmdkDiskProvisioningType": "SAME_AS_SOURCE",
        "replicateVmHardware": true,
        "role": "PRODUCTION",
        "vmReplicationId": "26e97e08267ef683",
        "vmToolsVersion": "12325",
        "vmdks": [
            {
                "included": true,
                "name": "Hard disk 2",
                "path": "SCSI (0:1)",
                "sizeInMB": 256000
            },
            {
                "included": true,
                "name": "Hard disk 1",
                "path": "SCSI (0:0)",
                "sizeInMB": 87040
            }
        ]
    }
]

```

-----------end snip------------  
I need to pass the results of the ID into collection 2 but I still need Name and Group to be available.  
Collection 2: Takes the ID and gets that information and in the response body it has address  
------begin snip-----

```auto
[
    {
        "adapterName": "Network Adapter 1",
        "adapterIndex": 1,
        "vcNetwork": {
            "id": "dvportgroup-621",
            "name": "FOO_ADDRESS"
        }
    }
]

```

-----------end snip------------  
my final result is a visualization table that has the following fields:  
Name, Group, ID, Address  
In each collection I have visualizer setup with a {{#each response}} loop and stores the necessary fields as a collection variable. but when I get to collection 2 it goes through the iterations but the visualizer doesn’t update. It just shows the first entry.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [August 2, 2023, 2:39pm UTC](https://community.postman.com/t/chaining-responses-and-visualizer-with-runner/50194/2 "2023-08-02T14:39:40Z")

</div>

When you say “Collection”, do you actually mean 2 requests within a collection?

So you need the name, groupName and ID from request 1 alongside the name from request 2 in a visualiser table?

How are you running request 2? How are you looping through the 1000 devices?

Can you copy and paste the code you have for the visualiser?

If I understand this correct, the way I would do this is as follows;

Single Collection with 1 request in to bring back all of the nodes.  
I would then control the loop in the tests tab of that request using sendRequest to make the second API call to retrieve the name\address.

I would initialize a new JavaScript array to store the results.  
I would have a foreach loop to loop through the nodes in the response.  
I would retrieve the Name, groupName and ID  
Still within the loop, I would use the Postman sendRequest() function for the second API call to retrieve the address\name.

I would then add all of the relevant elements as an object to the array.

Once the loop is finished, I would then use the new array as the input for the visualizer.

**Tip** : Use the pre-formatted text option in the editor when posting code, JSON or responses. As it stops everything from being aligned to the left and hard to read.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [August 2, 2023, 3:04pm UTC](https://community.postman.com/t/chaining-responses-and-visualizer-with-runner/50194/3 "2023-08-02T15:04:06Z")

</div>

Here is an example of creating an array, looping through the response, and formatting for the visualizer. If you have a 1000 entries, use the HTML I’ve provided as it has a sticky header for the visualizer.

```auto
const response = pm.response.json();

let array = []; // initialise array

response.forEach(node => {
    let groupName = node.groupName
    let id = node.id
    let name = node.name
    // use sendRequest here to call your second API
    let obj = {
        groupName: groupName,
        id: id,
        name: name
        // include the element from the second API here.
    }
    array.push(obj); // push the current object into the array
});

console.log(array);

var template = `

    <style type="text/css">
        .tftable {font-size:14px;color:#333333;width:100%;border-width: 1px;border-color: #87ceeb;border-collapse: collapse;}
        .tftable th {font-size:18px;background-color:#87ceeb;border-width: 1px;padding: 8px;border-style: solid;border-color: #87ceeb;text-align:left;position:sticky;top:0;}
        .tftable tr {background-color:#ffffff;}
        .tftable td {font-size:14px;border-width: 1px;padding: 8px;border-style: solid;border-color: #87ceeb;}
        .tftable tr:hover {background-color:#e0ffff;}
    </style>
    
    <table class="tftable" border="1">

        <tr>
            <th>Group Name</th>
            <th>Name</th>
            <th>ID</th>
        </tr>

        {{#each response}}
            <tr>
                <td>{{groupName}}</td>
                <td>{{name}}</td>
                <td>{{id}}</td>
            </tr>
        {{/each}}

    </table>

`;

pm.visualizer.set(template, {
    response: array // use the array for the visualizer input
});

```

You just need to add the code for the sendRequest() as I can’t mimic that easily.

[Postman JavaScript reference | Postman Learning Center](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#sending-requests-from-scripts)

---

<div class="post-metadata">

**Author:** ![alcsmith](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/alcsmith/32/16489_2.png) [@alcsmith](https://community.postman.com/u/alcsmith)\
**Post date:** [August 2, 2023, 3:40pm UTC](https://community.postman.com/t/chaining-responses-and-visualizer-with-runner/50194/4 "2023-08-02T15:40:23Z")

</div>

this is is the test for the 1st collection

```auto
const responseData=pm.response.json();
pm.collectionVariables.set("vmRep", pm.response.json()[0].id);
pm.collectionVariables.set("CGname", pm.response.json()[0].groupName);
pm.collectionVariables.set("VM", pm.response.json()[0].name);

var template = `
<style type="text/css">
        .tftable {font-size:11px;color:#333333;width:100%;border-width: 1px;border-color: #87ceeb;border-collapse: collapse;}
        .tftable th {font-size:14px;background-color:#87ceeb;border-width: 1px;padding: 8px;border-style: solid;border-color: #87ceeb;text-align:left;}
        .tftable tr {background-color:#ffffff;}
        .tftable td {font-size:11px;border-width: 1px;padding: 8px;border-style: solid;border-color: #87ceeb;}
        .tftable tr:hover {background-color:#e0ffff;}
    </style>
    <table class="tftable" border="1">
        <tr>
            <th>VM Name</th>
            <th>Consistency Group</th>
			<th>Replication ID</th>
			<th>Source Network</th>
        </tr>
        {{#each response}}
            <tr>
                <td>{{name}}</td>
				<td>{{groupName}}</td>
				<td>{{id}}</td>
                <td></td>
            </tr>
        {{/each}}
    </table>
`;

// Set visualizer
pm.visualizer.set(template, {
    // Pass the response body parsed as JSON as `data`
    response: pm.response.json()
});

```

---

<div class="post-metadata">

**Author:** ![alcsmith](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/alcsmith/32/16489_2.png) [@alcsmith](https://community.postman.com/u/alcsmith)\
**Post date:** [August 2, 2023, 3:41pm UTC](https://community.postman.com/t/chaining-responses-and-visualizer-with-runner/50194/5 "2023-08-02T15:41:04Z")

</div>

and this is the test for the 2nd collection

```auto
var template = `
<style type="text/css">
        .tftable {font-size:11px;color:#333333;width:100%;border-width: 1px;border-color: #87ceeb;border-collapse: collapse;}
        .tftable th {font-size:14px;background-color:#87ceeb;border-width: 1px;padding: 8px;border-style: solid;border-color: #87ceeb;text-align:left;}
        .tftable tr {background-color:#ffffff;}
        .tftable td {font-size:11px;border-width: 1px;padding: 8px;border-style: solid;border-color: #87ceeb;}
        .tftable tr:hover {background-color:#e0ffff;}
    </style>
    <table class="tftable" border="1">
        <tr>
            <th>VM Name</th>
            <th>Consistency Group</th>
			<th>Replication ID</th>
			<th>Source Network</th>
        </tr>
        {{#each response}}
            <tr>
                <td>{{VM}}</td>
				<td>{{CGname}}</td>
				<td>{{vmRep}}</td>
                <td>{{vcNetwork.name}}</td>
            </tr>
        {{/each}}
    </table>
`;

// Set visualizer
pm.visualizer.set(template, {
    // Pass the response body parsed as JSON as `data`
    response: pm.response.json()
});

```

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [August 2, 2023, 3:49pm UTC](https://community.postman.com/t/chaining-responses-and-visualizer-with-runner/50194/6 "2023-08-02T15:49:23Z")

</div>

Your first request is only bringing back the first object in the array. You are using the array index [0] and then setting those elements as collectionVariables.

Your second request is again only bringing back a single record.

If you need to loop through a 1000 entries, then you will need a loop similar to what I posted previously to combine the two elements.
