Setting environment variables from within visualizer templates

I’ve created a topic before, looking for a way to interact with Postman from within a template. It seemed that wasn’t possible.
For a new personal project I’m looking for something similar, yet my hopes are that it’s closer to what postman currently offers.

I know you can set environment variables using pm.environment.set(). I’ve also found out you can do this in a template using ${pm.environment.set(‘key’, ‘value’)}. What I would like to do though, is setting the value with javascript. But so far all my efforts are without result.

It would be incredibly handy if this was possible. Currently I have a lot of ‘list’ calls that I visualize with a template. There are lots of other calls that need information from that list call (such as an id). Now, from my other topic I know these calls can’t be invoked directly, but I was hoping that maybe I could create a button in my template that would allow me to set an environment variable (with the value of an id) so that I could achieve the same thing using two clicks instead of one. ( that way I could simply use the environment variable in those subsequent calls instead of having to copy-paste data into parameters the whole time)

Is this possible?

Hi @themarty! This is an interesting one. :thinking: Have you tried anything with pm.getData?

Actually can I ask where the id value you’re trying to set as the var value is coming from?

Hi @suesmith
Thank you for your reply.

I have tried adding a function to the response data, but it simply gets ‘lost’ - meaning: if I console.log the entire object in my template, then the function is not there anymore. I’ve tried it with a simple function (prop = function() { x=1; }), but that already didn’t work, let alone trying to pass something more complicated.
I wouldn’t know of any other way to use pm.getData to be able to set a postman environment variable - but if you have suggestions, then they are more then welcome! :slight_smile:

The id value is in the resultset of an API call. So in my template I have something like this:

{{#each response}}
<tr>
<td>{{id}}</td>
<td>{{name}}</td>
[…]
</tr>
{{/each}}

And what I want to accomplish is that when a User clicks on the value of the id, a corresponding environment value is set. For example something like pm.environment.set("user_id", {{id}});
If that would work then my other Postman calls needing that user-id could be written as (for example) {{API_DOMAIN}}{{BASE_URL}}/user/{{user_id}}/. Without the need of having to manually copy-paste the user-id of the user you’re interested in, from the list of users.

Thanks for the additional info, I had a mess about and found the same as yourself… If I come across a workaround I’ll let you know but sadly I suspect this is going to be problematic atm based on these feature requests:

Sorry I can’t be more help!