In the Collection level pre-request script, declare an object without using the var, let or const keywords. The object is then attached to the global sandbox and is available in child context of that collection.
Why it works only for getting variables? When in this same manner I have utils object with a method which changes environmentVariables it doesn’t work. I need to pass explicitly in pre-request script of a request pm to make it work.