# Environment Variable values not getting set from grand-parent folder's Pre-Request Script

**URL:** <https://community.postman.com/t/environment-variable-values-not-getting-set-from-grand-parent-folders-pre-request-script/35001>\
**Category:** Help Hub\
**Tags:** tests, variables, collections, json, pre-request-script\
**Created:** [May 4, 2022, 3:03pm UTC](https://community.postman.com/t/environment-variable-values-not-getting-set-from-grand-parent-folders-pre-request-script/35001 "2022-05-04T15:03:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![edgetonjw](https://avatars.discourse-cdn.com/v4/letter/e/ad7895/32.png) [@edgetonjw](https://community.postman.com/u/edgetonjw)\
**Post date:** [May 4, 2022, 3:03pm UTC](https://community.postman.com/t/environment-variable-values-not-getting-set-from-grand-parent-folders-pre-request-script/35001/1 "2022-05-04T15:03:19Z")

</div>

Hi Postman community, my organization has been slowly adopting Postman for regression testing. This is the first issue we’ve run into that I am hoping to verify if it’s a bug we’re seeing **or** is unsupported in Postman.

_My question_:  
Are there any known issues or limitations to environment variables getting set in a folder’s Pre-Request Script code that would prevent the value being accessible in a request nested down in the folder structure?

_Details_:  
**Collection structure**  
 → Parent folder (utils.myFunction() method defined in Pre-Request Script)  
----\> Child folder  
--------\> Grandchild folder (contains POST request calling utils.myFunction(varName, varValue) in Tests)

**Code logic (simplified):**

1. POST request calls utils.myFunction(varName, varValue) in Tests

2. In utils.myFunction:

```auto
pm.environment.set(varName, varValue);
pm.environment.get(varName); 

```

_This seems to be working as expected within the function_

1. Returning back to the POST request after calling utils.myFunction method:

```auto
pm.environment.get(varName);

```

\*This **DOES NOT** seems to be working - it is returning the prior value of varName before calling utils.myFunction)

_How I found the problem_:  
We’re trying to consolidate redundant code within multiple requests into a common library of shared functions, which we’ve placed in the Pre-Request Script section of a higher-up folder within the collection. The collection starting failing after making this change.

_I’ve already tried_:

- Searching online to verify if this is a known issue or unsupported option within Postman
- Using console.log() messages throughout the code to verify the observations noted above
- Verified the environment variable’s “Initial Value” is empty

---

<div class="post-metadata">

**Author:** ![edgetonjw](https://avatars.discourse-cdn.com/v4/letter/e/ad7895/32.png) [@edgetonjw](https://community.postman.com/u/edgetonjw)\
**Post date:** [May 18, 2022, 1:44pm UTC](https://community.postman.com/t/environment-variable-values-not-getting-set-from-grand-parent-folders-pre-request-script/35001/2 "2022-05-18T13:44:16Z")

</div>

Hello, just checking back if anyone is able to provide some insight on this issue. Thanks!

---

<div class="post-metadata">

**Author:** ![archerZ](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@archerZ](https://community.postman.com/u/archerZ)\
**Post date:** [June 12, 2022, 6:51pm UTC](https://community.postman.com/t/environment-variable-values-not-getting-set-from-grand-parent-folders-pre-request-script/35001/3 "2022-06-12T18:51:44Z")

</div>

hi @edgetonjw,

Did you try using collection variables? This might solve your issue and the variable scope is within the collection.

---

<div class="post-metadata">

**Author:** ![cristian-fleischer](https://avatars.discourse-cdn.com/v4/letter/c/e0b2c6/32.png) [@cristian-fleischer](https://community.postman.com/u/cristian-fleischer)\
**Post date:** [September 13, 2023, 3:30pm UTC](https://community.postman.com/t/environment-variable-values-not-getting-set-from-grand-parent-folders-pre-request-script/35001/4 "2023-09-13T15:30:00Z")

</div>

Encountering the same issue.  
If there is some technical limitation that precludes us from setting environment variables in this way it’s fine, but please document this in the official documentation.

---

<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:** [September 13, 2023, 4:03pm UTC](https://community.postman.com/t/environment-variable-values-not-getting-set-from-grand-parent-folders-pre-request-script/35001/5 "2023-09-13T16:03:07Z")

</div>

Hey @edgetonjw 👋

Are you able to give a visual example of what that looks like and the full `utils` function that you have created at the Collection level?

---

<div class="post-metadata">

**Author:** ![xedinunknown](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/xedinunknown/32/27779_2.png) [@xedinunknown](https://community.postman.com/u/xedinunknown)\
**Post date:** [March 8, 2024, 8:39pm UTC](https://community.postman.com/t/environment-variable-values-not-getting-set-from-grand-parent-folders-pre-request-script/35001/6 "2024-03-08T20:39:27Z")

</div>

I would really like to know this too, because I am experiencing the same exact thing: a function in a parent’s Pre-Request Script sets environment variables, which persist when retrieved within that same function, but not when retrieved in a child’s (in my case, request’s) Tests script.

---

<div class="post-metadata">

**Author:** ![gbadebo-bello](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gbadebo-bello/32/12175_2.png) [@gbadebo-bello](https://community.postman.com/u/gbadebo-bello)\
**Post date:** [March 11, 2024, 2:26pm UTC](https://community.postman.com/t/environment-variable-values-not-getting-set-from-grand-parent-folders-pre-request-script/35001/7 "2024-03-11T14:26:26Z")

</div>

Hey all.

I’m not able to replicate this issue on my end. Can you confirm that:

- The env variable you set was indeed set in your environments tab
- Before trying to use the set variables, you have set the right “active” environment and that,
- You’re using the right environment variable key

---

<div class="post-metadata">

**Author:** ![Andrewdeakins](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Andrewdeakins](https://community.postman.com/u/Andrewdeakins)\
**Post date:** [August 23, 2024, 9:45am UTC](https://community.postman.com/t/environment-variable-values-not-getting-set-from-grand-parent-folders-pre-request-script/35001/8 "2024-08-23T09:45:22Z")

</div>

I can replicate this, it seems the postman instance is not global. If i have a function in a pre request script on a parent folder that sets a variable (can be global, environment etc) and try and read this using pm.get in the child, the variable is undefined. If i pass the pm instance to the parent function to and use this to set the variable and then read it using the same instance in the child it works and this is for every variable type. I would have assumed that using the globals collection would have persisted across folders. Not sure if i’m missing something should i be using pm.globals.set ?

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [August 23, 2024, 10:48am UTC](https://community.postman.com/t/environment-variable-values-not-getting-set-from-grand-parent-folders-pre-request-script/35001/9 "2024-08-23T10:48:32Z")

</div>

@Andrewdeakins

If you want to create a global function that needs to incorporate pm methods, then you need to pass the pm function. (Otherwise pm will be undefined within that global function).

![image](https://us1.discourse-cdn.com/getpostman/original/3X/5/f/5f01255be2728c48d52d66ee1d2bc4ee8c7d692c.png)

**Pre-request on folder 1**

```auto
utils = {
    statusCode: function (pm, code) {
        pm.test(`Status code is ${code}`, () => {
            pm.response.to.have.status(code);
        })
        return utils;
    },
    response: function (pm) {
        console.log(pm.response.json());
        return utils;
    },
    environment: function (pm, variable, value) {
        pm.environment.set(variable, value);
        return utils;
    }
};

```

**Post-response in Get request**

```auto
utils.response(pm);
utils.statusCode(pm, 200);

utils.environment(pm, "testVar", "testValue")
console.log(pm.environment.get("testVar")); // testValue

utils.environment(pm, "testVar", "testValue2")
console.log(pm.environment.get("testVar")); // testValue2

```

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

![image](https://us1.discourse-cdn.com/getpostman/original/3X/f/5/f54c1f1c1a1a7100a3d3e00c83ff81be29794387.png)

---

<div class="post-metadata">

**Author:** ![Andrewdeakins](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Andrewdeakins](https://community.postman.com/u/Andrewdeakins)\
**Post date:** [August 23, 2024, 12:27pm UTC](https://community.postman.com/t/environment-variable-values-not-getting-set-from-grand-parent-folders-pre-request-script/35001/10 "2024-08-23T12:27:24Z")

</div>

Hi thanks, yep found out by passing it to establish if pm was global or not. Was wondering if that was correct as it feels a little odd having global collections that are tied to an instance.

Good to know other people are doing this as was wondering if it was correct. Thanks for the help
