Set Environment inside Visualizer Template

Hi,

is it possible to manipulate the postman environment inside the script of the visualizer? I tried to use pm.environment.set() as I would on the regular test script. But that doesn’t seem to do anything.

What I’m trying to do is to use the visualizer to show a simple table of the response data. Every data point has a name and an ID. When clicking on a row of the table, the ID of the data point should be added to the postman environment, so that it can be used in other requests that require that ID. For now we always use the first entry as the ID, so it would be cool to have that kind of simple selection mechanism. :slight_smile:

This is my example visualizer so far:

const template = `<table id="tableId" class="tftable" border="1">
    <tr>
        <th>Name</th>
        <th>ID</th>
    </tr>
    
    {{#each response.values}}
    {{#each value}}
    <tr id="{{id}}"">
        <td id={{@key}}>{{name}}</td>
        <td>{{id}}</td>
    </tr>        
    {{/each}}{{/each}}
</table>    

<script>
    function addRowHandlers() {
        var table = document.getElementById("tableId");
        var rows = table.getElementsByTagName("tr");
        
        for (i = 0; i < rows.length; i++) {
            var currentRow = table.rows[i];
            var createClickHandler = 
                function(row) {
                    return function() { 
                        var cell = row.getElementsByTagName("td")[0];
                        var id = cell.innerHTML;
                        alert(id);
                        pm.environment.set("FOOBAR", id);
                    };
                };
            currentRow.onclick = createClickHandler(currentRow);
        }
    }
    
    window.onload = addRowHandlers();
</script>`

pm.visualizer.set(template, {
    response: pm.response.json()
});

Hi @Sebaestschjin and welcome here :smiley:

What you’re asking isn’t feasible at the moment. Someone already opened a feature request along these lines, you may want to give it a +1!