# Global variables undefined in pre-request script

**URL:** <https://community.postman.com/t/global-variables-undefined-in-pre-request-script/21685>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [March 22, 2021, 9:20am UTC](https://community.postman.com/t/global-variables-undefined-in-pre-request-script/21685 "2021-03-22T09:20:30Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![m.hernik](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@m.hernik](https://community.postman.com/u/m.hernik)\
**Post date:** [March 22, 2021, 9:20am UTC](https://community.postman.com/t/global-variables-undefined-in-pre-request-script/21685/1 "2021-03-22T09:20:30Z")

</div>

Hi.  
I have a pre-request script that is generating a JWT auth token. Variables are taken from globals variable pool.  
When I change workspace very often those variable are unreadable, and therefore undefined.  
To solve that I need to edit global envs and just save the changes without any changes.

 ![2021-03-22_10-08](https://us1.discourse-cdn.com/getpostman/original/3X/5/8/58ef23e3ca6a716c6c2fcff8c4b1f0a3b7cbe254.png)

Postman v8.0.7 Working on Ubuntu

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 22, 2021, 11:57am UTC](https://community.postman.com/t/global-variables-undefined-in-pre-request-script/21685/2 "2021-03-22T11:57:52Z")

</div>

Hey @m.hernik 👋

The Global scope is for each workspace so if you have `Variable A in Workspace 1` you wouldn’t see that Variable when switching Workspaces.

Have you tried moving the scope of that Variable down to the Environment level, that way it would be part of `Prod`?

---

<div class="post-metadata">

**Author:** ![m.hernik](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@m.hernik](https://community.postman.com/u/m.hernik)\
**Post date:** [March 22, 2021, 1:08pm UTC](https://community.postman.com/t/global-variables-undefined-in-pre-request-script/21685/3 "2021-03-22T13:08:49Z")

</div>

Hi Danny.  
I’m not sure I follow.  
So I have Variable A in workspace 1, but when I switch to workspace 2 and back to 1 I won’t be able to access that anymore?  
I don’t have problem with variable per environment so I’m sure that when I move this will variable down this will work, but then the point of having this var globally for all envs is gone, right?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 22, 2021, 1:48pm UTC](https://community.postman.com/t/global-variables-undefined-in-pre-request-script/21685/4 "2021-03-22T13:48:10Z")

</div>

Forgive the Workspaces in the images, just using this as a demo. 😄

All the `Global` variables for a Workspace, are _only_ available within _that_ Workspace.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/7/c7d84285ff77640f28722333b452c74ccfa717d0.png)

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/0/c/0cfb3444fe4c82a2dab7312ccb20804c26a2cd6b.png)

Those variables are **not** globally accessible across _all_ workspaces.

If the Global variable is available in the workspace where you’re accessing it using `pm.globals.get('var_name')` then it should be resolved correctly but if it’s not part of that Workspace, it would be `undefined`.

---

<div class="post-metadata">

**Author:** ![m.hernik](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@m.hernik](https://community.postman.com/u/m.hernik)\
**Post date:** [March 22, 2021, 2:13pm UTC](https://community.postman.com/t/global-variables-undefined-in-pre-request-script/21685/5 "2021-03-22T14:13:56Z")

</div>

> All the `Global` variables for a Workspace, are _only_ available within _that_ Workspace.

That’s clear. But I am in the workspace in which I have environments `dev/stage/prod` and all those are sharing the same global variable: `serviceAuthBaseUrl`.

The problem is, that very often when I switch between workspaces this variable is undefined. I have to enter the edition screen and overwrite the value for it to be seen by pre-request script.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 22, 2021, 2:18pm UTC](https://community.postman.com/t/global-variables-undefined-in-pre-request-script/21685/6 "2021-03-22T14:18:18Z")

</div>

If it’s in the initial value and saved - It shouldn’t just `unset` itself, unless there is something in your Collection doing that.

It’s going to be difficult to suggest anything here because I can’t see what you’re doing or know what’s part of the Collection/Workspace. 😄

---

<div class="post-metadata">

**Author:** ![Pawel\_Zalewski](https://avatars.discourse-cdn.com/v4/letter/p/3d9bf3/32.png) [@Pawel\_Zalewski](https://community.postman.com/u/Pawel_Zalewski)\
**Post date:** [April 21, 2021, 9:04am UTC](https://community.postman.com/t/global-variables-undefined-in-pre-request-script/21685/7 "2021-04-21T09:04:32Z")

</div>

Seems like it stopped working for me as well.  
Even more it stopped overriding global variables with environment ones.

I have pre request script with pm.globals.get(“url”).  
I logged url in that script and was undefined.  
Then I changed environment (diferent url) and it logged GLOBAL value.  
So it is related to what thread owner said.

The workspace is about half a year old so it worked before.

---

<div class="post-metadata">

**Author:** ![de9ka](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/de9ka/32/10142_2.png) [@de9ka](https://community.postman.com/u/de9ka)\
**Post date:** [October 21, 2021, 8:59pm UTC](https://community.postman.com/t/global-variables-undefined-in-pre-request-script/21685/8 "2021-10-21T20:59:41Z")

</div>

I have the same problem. Variables with scope “collection variables” or “global” are not saved or updated when changed in Pre-request scripts. Probably a bug ☹

---

<div class="post-metadata">

**Author:** ![soroush-saffari](https://avatars.discourse-cdn.com/v4/letter/s/e79b87/32.png) [@soroush-saffari](https://community.postman.com/u/soroush-saffari)\
**Post date:** [November 28, 2021, 12:04am UTC](https://community.postman.com/t/global-variables-undefined-in-pre-request-script/21685/9 "2021-11-28T00:04:32Z")

</div>

Hi Danny! Any updates on this?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [December 1, 2021, 12:35pm UTC](https://community.postman.com/t/global-variables-undefined-in-pre-request-script/21685/10 "2021-12-01T12:35:39Z")

</div>

If this is for me, as it’s been a while and there are very few examples on the thread of what is going on, it’s going to be difficult to have any input on this topic.

Are you facing a similar problem? Can you provide examples or a short recording of the issue you’re facing in the latest version?

---

<div class="post-metadata">

**Author:** ![rockvcp](https://avatars.discourse-cdn.com/v4/letter/r/a4c791/32.png) [@rockvcp](https://community.postman.com/u/rockvcp)\
**Post date:** [May 11, 2022, 6:23am UTC](https://community.postman.com/t/global-variables-undefined-in-pre-request-script/21685/11 "2022-05-11T06:23:21Z")

</div>

i think i resolved the issue.  
from `pm.globals.get('apiKeyTest')` to `pm.environment.get('apiKeyTest')`  
not sure this is exactly the issue you guys encountered. but i think it is similar from the issue i have encountered.

---

<div class="post-metadata">

**Author:** ![RKS](https://avatars.discourse-cdn.com/v4/letter/r/50afbb/32.png) [@RKS](https://community.postman.com/u/RKS)\
**Post date:** [June 28, 2022, 3:01pm UTC](https://community.postman.com/t/global-variables-undefined-in-pre-request-script/21685/12 "2022-06-28T15:01:15Z")

</div>

I believe I found a solution:  
steps to use it

1. go to Global variable, and double click on your variable
2. Postman will give you an option to make it either global or environmental
3. select the global option, Postman will cover your variable into {{}} braces, then it can be used as global variable  
 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/7/770f31f15978801d9af1febc36ca62bd3eabe796.png)

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [August 24, 2023, 4:20pm UTC](https://community.postman.com/t/global-variables-undefined-in-pre-request-script/21685/13 "2023-08-24T16:20:56Z")

</div>

Closed on August 24, 2023, 4:20pm UTC


