My issue is described in the last comment in that gist, under [email protected]. To summarize again here - the gist creates a variable and an environment variable in a pm.sendRequest() function as part of a larger function. When I run this manually, the variables persist after this function and can be used in the body of my request, and everything works fine.
When I run it as a scheduled run in Postman Cloud, however, it looks like the variables are created in pm.sendRequest() as console logs show they both exist, but they do not persist after being created there and after the function is done a console.log shows that they are undefined.
I can鈥檛 figure out how to fix this error. Any thoughts on the issue and what is causing this divergent behavior between manual and scheduled runs would be helpful.
Your problem is likely due to the way certain variables are treated differently by Postman Cloud vs. a local Postman session. It could be related to the different behavior of Initial and Current variable values.
I experienced a similar issue where I needed to persist Environment variables across different requests in separate collections. Because pm.environment.set only sets the Current value of a variable, and how Current values don鈥檛 persist beyond requests or collection runs, this was a challenge. The solution I found was to set the Initial variable values using a separate PUT request to update the Environment. It does so by sending your Environment data in JSON format. Note that this will completely overwrite your Environment, so you need to include all of your needed variables and their properties in the JSON. Tip: you can export your Environment as a JSON file from the Environments section located on the sidebar.
What worked for me was to save the value I needed from my script as a local variable. I then set the subsequent request in my collection run as the Update Environment PUT request, which references the local variable (using double curly braces) in the JSON to set my Environment variable Initial value. Now, any subsequent references of my Environment variable have that value.