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

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

hi @edgetonjw,

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