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

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:

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:
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
1 Like

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

1 Like

hi @edgetonjw,

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

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.

1 Like

Hey @edgetonjw :wave:

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?

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.

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