Want to overwrite the current value of Environment

I’m using Collection Environment in Postman. I overwrite the values of the Environment using an API. After that, when I make a request using that Environment, instead of the Initial value, the Current value is being used. When I execute Reset All from the button, the Initial value is inserted into the Current value. I want to automate this. I’ve set up the Pre-request script as follows:

var keys = Object.keys(pm.environment.toObject());

keys.forEach(function(key) {
    value = pm.environment.get(key)
    pm.environment.unset(key)
    pm.environment.set(key,value)
});

console.log(pm.environment.get("update_target"))

However, this doesn’t behave as intended (it outputs the value of the current value). How can I reset the Current value?"

Hi @payload-geoscienti27. Welcome to the Postman Community.

I am not sure I clearly understand your question. pm.environment.set should indeed update your current values.

Variables in the initial value are synced with your team and Postman cloud. Variables in the current values are only available locally.

The current value always inherits from the initial value and you can only control the values of the current values via scripting. If your intention is not to make the variables you set available to everyone on your team, you should leave the initial values empty. This way, the current value does not inherit the values in the initial value.

There are two APi methods available to pm.environment but both will clear the variables, not reset the current value.

Using environment variables in scripts | Postman Learning Center

pm.environment.unset() will unset a single variable, and pm.environment.clear() will clear all variables.

Probably not what you want in this scenario.

Apart from looping through and just resetting the variables to null or β€œβ€, I can’t see any way of resetting the current value. And even then, that is not the same as clearing the variable which would then allow it to sync with the initial value again.

What you can do, but is more code is use the Postman Environments API.

This will allow you to read the initial value, and use that to loop through and reset the current values.

Manage Your Postman Environments with the Postman API | Postman Blog

pm.environment.set should indeed update your current values.

I’m sure it was updated.

Is it possible to get the initial value with pre script?

I change pre-script

// registed environment. key:update_tartget , initial value:update_value , current value:update_value_new

var keys = Object.keys(pm.environment.toObject());

keys.forEach(function(key) {
    value = pm.environment.get(key)
    pm.environment.set(key,null)
    console.log(pm.environment.get("update_target"))
    pm.environment.set(key,value)
});

console.log(pm.environment.get("update_target"))

However, the output to console looks like this:

null
update_value

I would like the following

null
update_value_new

Is it difficult?

If you select reset all on the environment screen, the current value will be overwritten with the initial value. However, it is tedious to execute the button every time, so I would like to achieve the same thing with pre-script.

I was able to do what I wanted!

thank you!

var environmentId = "aaa";
var apiKey = "bbb";

pm.sendRequest({
    url: "https://api.getpostman.com/environments/" + environmentId,
    method: 'GET',
    header: {
        'X-Api-Key': apiKey
    }
}, function (err, res) {
    if (err) {
        console.log(err);
    } else {
        var initialValues = res.json().environment.values;
        initialValues.forEach(function(variable) {
            if (variable.enabled) {
                pm.environment.set(variable.key,variable.value)
            }
        });
    }
});
1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.