[Postman v11] Run a global function from a child folder

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.
image

Note: Before upgrading to Postman 11 it worked perfectly.

Probably one for someone from Postman to answer.

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.

Hey @monica.m :wave:

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.

1 Like

Hello @monica.m,

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.

utils = {
    version: '1.0.2',
    base64url : function(source) {
        const crypto = require('crypto-js');
        // Encode in classical base64
        encodedSource = crypto.enc.Base64.stringify(source)
        
        // Remove padding equal characters
        encodedSource = encodedSource.replace(/=+$/, '')
        
        // Replace characters according to base64url specifications
        encodedSource = encodedSource.replace(/\+/g, '-')
        encodedSource = encodedSource.replace(/\//g, '_')
        
        return encodedSource
    },
    upperCaseFirst : function(str) {
        return str.charAt(0).toUpperCase() + str.slice(1);
    },
    getCurrentYear : function() {
        const now = new Date();
        return now.getFullYear();
    },
    getNextYear : function() {
        return utils.getCurrentYear() + 1;
    }
};

Next, in subfolders, I use “utils.getNextYear()”.

I always use a “version” property, because I don’t use the new v11 “package” in Postman.
(At the moment, it can’t be used with Newman)

Hope this helps you.

Best

Didier

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.