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?"
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.
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.
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.