How can I run a global function in a separate folder than the one where it is declared?
Ex: In the pre-request script of the collection I set the following:
pm.globals.set(“collectionGlobalFunction”, colGlobalFunc = () => 3 + 5);
If I call this variable in the same pre-request script of the collection , like this, it works:
console.log("This is global function called from collection pre-request : ");
console.log(pm.globals.get(“collectionGlobalFunction”));
But if I’m using the console logs into a pre-request of a child folder (in the collection), it doesn’t work:
console.log("This is global function called from step with console output: ");
console.log(pm.globals.get(“collectionGlobalFunction”));
It gives null.
Note: Before upgrading to Postman 11 it worked perfectly.
I thought that code only executed from the current request, the current folder, or at the collection level.
If you have folders within folders, I didn’t think it would execute the code in the pre-requests for all of the folders in the structure. Only the current folder.
If it used to work, then fair play, hence I suspect someone from Postman will be able to advise further.
Having no prior background into your context and only seeing a small sample of where you have used it and not seeing the full Collection to know what colGlobalFunc is doing - I have no comment about this working or not working prior to V11.
I would suggest looking at using the Package Library feature in V11 for scripts that you want to use at a Global level.
All global functions that had been create prior to V11 were done as a workaround for Postman not having this capability in place.
All variables declare in a pre-script are global in the sub-hierarchy as long as you don’t use var/const/let in the declaration.
This works with postman v10 & v11.
For function, I use something like this in the top collection.