# Set variable's current value instead of initial value in script

**URL:** <https://community.postman.com/t/set-variables-current-value-instead-of-initial-value-in-script/2713>\
**Category:** Help Hub\
**Tags:** scripts\
**Created:** [September 18, 2018, 8:38pm UTC](https://community.postman.com/t/set-variables-current-value-instead-of-initial-value-in-script/2713 "2018-09-18T20:38:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![denniskugelmann](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@denniskugelmann](https://community.postman.com/u/denniskugelmann)\
**Post date:** [September 18, 2018, 8:38pm UTC](https://community.postman.com/t/set-variables-current-value-instead-of-initial-value-in-script/2713/1 "2018-09-18T20:38:21Z")

</div>

Hi,

Is it somehow possible to set in a pre-request script the current value of a global/environment variable instead of the initial value?

My script:

```auto
const refreshRequest = {
  url: 'https://securetoken.googleapis.com/v1/token?key=' + pm.globals.get("webApiKey"),
  method: 'POST',
  header: 'Content-Type:application/json',
  body: {
    mode: 'application/json',
    raw: JSON.stringify(
        {
        	grant_type: 'refresh_token',
        	refresh_token: pm.globals.get("refreshToken")
        })
  }
};

var getToken = true;

if (!pm.globals.get('expirationTime') || 
    !pm.globals.get('idToken')) {
    console.log('Token or expiry date are missing')
} else if (pm.globals.get('expirationTime') <= (new Date()).getTime()) {
    console.log('Token is expired')
} else {
    getToken = false;
    console.log('Token and expiry date are all good');
}

if (getToken) {
    pm.sendRequest(refreshRequest, (err, res) => {
        console.log(err ? err : res.json());
        if (!err) {
            console.log('Saving the tokens and expiry date')
            var responseJson = res.json();
            pm.globals.set('idToken', responseJson.id_token)
            pm.globals.set('refreshToken', responseJson.refresh_token)
            
            
            var expiryDate = new Date();
            expiryDate.setSeconds(expiryDate.getSeconds() + responseJson.expires_in);
            pm.globals.set('expirationTime', expiryDate.getTime());
        }
    })
}

```

As you can see I have several `pm.globals.set` calls in the script, which set rather security relevant variables, like id and refresh tokens.  
I don’t want these variables to be synced to all colleagues.  
When I run `pm.globals.set('refreshToken', responseJson.refresh_token)` this does not only set the current value, it set’s the initial value as well, what I try to prevent.

How can I achieve this?

I hope for some good tips,  
Dennis

---

<div class="post-metadata">

**Author:** ![postman-harryi3t](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/postman-harryi3t/32/947_2.png) [@postman-harryi3t](https://community.postman.com/u/postman-harryi3t)\
**Post date:** [September 22, 2018, 3:33am UTC](https://community.postman.com/t/set-variables-current-value-instead-of-initial-value-in-script/2713/2 "2018-09-22T03:33:39Z")

</div>

Hi Dennis,

From any script (collection/request pre-request/test), it is only possible to set the current value. Depending on your setting `auto persist variables values` the modified current value will be copied to the initial value. If that is something you don’t want, you can turn it off.

Here’s a simple flowchart from [our docs](https://www.getpostman.com/docs/v6/postman/collection_runs/using_environments_in_collection_runs) explaining it visually

 ![Using_environments_in_collection_runs](https://us1.discourse-cdn.com/getpostman/original/3X/d/c/dc60bfbb2970167681bac9311f3baf30bb188dcc.jpeg)

Here’s where you can find this setting

 ![Postman](https://us1.discourse-cdn.com/getpostman/original/3X/3/5/3566f42752c1e920096b0fc4843b4443a018ff7d.png)

---

<div class="post-metadata">

**Author:** ![denniskugelmann](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@denniskugelmann](https://community.postman.com/u/denniskugelmann)\
**Post date:** [September 22, 2018, 12:22pm UTC](https://community.postman.com/t/set-variables-current-value-instead-of-initial-value-in-script/2713/3 "2018-09-22T12:22:57Z")

</div>

Oh, thanks,

this is exactly what I was looking for 🙂

I looked in the documentation for something like this.  
Unfortunately, the documentation is even if it’s easily understandable, sometimes a bit unintuitively organized.

I often had to look through a lot of pages to find the relevant info.  
I hope that Postman gets it’s already good documentation as good as the awesome Postman product! 😃

---

<div class="post-metadata">

**Author:** ![postman-harryi3t](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/postman-harryi3t/32/947_2.png) [@postman-harryi3t](https://community.postman.com/u/postman-harryi3t)\
**Post date:** [September 26, 2018, 7:43am UTC](https://community.postman.com/t/set-variables-current-value-instead-of-initial-value-in-script/2713/4 "2018-09-26T07:43:10Z")

</div>

Thanks, Dennis

Sure, I will let our team know about this. We are currently working on improving our docs.

---

<div class="post-metadata">

**Author:** ![clemens.peters](https://avatars.discourse-cdn.com/v4/letter/c/5fc32e/32.png) [@clemens.peters](https://community.postman.com/u/clemens.peters)\
**Post date:** [September 18, 2019, 9:00am UTC](https://community.postman.com/t/set-variables-current-value-instead-of-initial-value-in-script/2713/5 "2019-09-18T09:00:45Z")

</div>

Is there a way to enforce this setting for a shared workspace?  
Otherwise if anyone in the team forgets this setting, tokens might be uploaded right (if you have a test, which uses setEnvironmentVariable)?

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [September 18, 2019, 9:06am UTC](https://community.postman.com/t/set-variables-current-value-instead-of-initial-value-in-script/2713/6 "2019-09-18T09:06:01Z")

</div>

@clemens.peters currently there’s no way to enforce this for a shared workspace.  
There’s already a [feature request](https://github.com/postmanlabs/postman-app-support/issues/5146) which has been filed for this.

You can subscribe to the thread for any future updates around that.

---

<div class="post-metadata">

**Author:** ![oblivion\_postman](https://avatars.discourse-cdn.com/v4/letter/o/94ad74/32.png) [@oblivion\_postman](https://community.postman.com/u/oblivion_postman)\
**Post date:** [October 6, 2021, 11:25am UTC](https://community.postman.com/t/set-variables-current-value-instead-of-initial-value-in-script/2713/7 "2021-10-06T11:25:50Z")

</div>

Hi,  
I’m using Postman v9.0.5, but in Settings menu under General tab, there isn’t ‘Automatically persist variable values’ option. How can I automatically update environment variable’s initial value with current value?  
Thanks in advance,

Andrea

---

<div class="post-metadata">

**Author:** ![brojenuel](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/brojenuel/32/12833_2.png) [@brojenuel](https://community.postman.com/u/brojenuel)\
**Post date:** [August 8, 2022, 12:47pm UTC](https://community.postman.com/t/set-variables-current-value-instead-of-initial-value-in-script/2713/8 "2022-08-08T12:47:45Z")

</div>

were can i find this setting? it seems does not exist version 9.27.2

---

<div class="post-metadata">

**Author:** ![meghamahant](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@meghamahant](https://community.postman.com/u/meghamahant)\
**Post date:** [November 3, 2023, 9:05am UTC](https://community.postman.com/t/set-variables-current-value-instead-of-initial-value-in-script/2713/9 "2023-11-03T09:05:45Z")

</div>

Hi Harendra,

I don’t see this setting in the latest version of Postman.  
Could you please guide how can this be fixed?

---

<div class="post-metadata">

**Author:** ![akarpuhov](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@akarpuhov](https://community.postman.com/u/akarpuhov)\
**Post date:** [October 3, 2024, 10:10am UTC](https://community.postman.com/t/set-variables-current-value-instead-of-initial-value-in-script/2713/10 "2024-10-03T10:10:47Z")

</div>

No ‘Automatically persist variable values’ option exists in Postman. I don’t know how to automatically update environment variable’s initial value with current value. Anyone can help in postman support?
