Collection variables not set after executing collections pre-request-script without errors

My question:
i have been trying to use functions in my collections pre-request script to create variables.
when i check for the variable inside the collections pre-request script its found,
howerver when i do the same afterwards its gone. i couldnt find it manually either.
i am quite new to postman, am i missing something here?

Details:
this is the script where i call the function from the collections pr script:

this is the pre-request script of the collection where i create the new variable:

this is the console output:
image

I’ve already tried:
i tried using global or enviornment varibles instead, but that did nothing.

what should i be doing differently?
im using version 10.0.1

Hi @pherrmann18

The issue here is that the pm.collectionVariables.get

pre-request script of the collection by default executes before every call within that collection. So I’m struggling to see why you would need to encase this in a function and call that function within the test script… (it would have already executed the collection pre-req by the time the test script runs).

If you remove the function and just run the code inside it works as you described…

image

You can also see that the variable get added and updated too;

hey @w4dd325 ,
my goal was to create a set of functions i could then use globally in that collection.
i dont want it to execute everything before every request.
this was more of an example.
is there some way to achieve that?

If you want to call a function from a pre-request script, consider the following (very basic) example using global variables. Might be an option.

Pre-request script.

postman.setGlobalVariable("functionA", () => {
    console.log("hello world")
});

Tests tab

Hello = eval(pm.globals.get("functionA"));
Hello();

@pherrmann18

This also works.

Pre-request script (can be set at the request or collection level).

Object.prototype.log = (msg) => {
    console.info(pm.info.requestName + " | " + msg);
};

Tests tab

pm.log("this is my log message");

To extend this.

The following will allow you to set a collectionVariable.

Pre-request script

Object.prototype.myGlobalFunction = function(myPm, key, value){
     myPm.collectionVariables.set(key, value);
}

Tests tab

_.myGlobalFunction(pm, "myKey", "myValue");

It does seem that Object.prototype can work.

Back to your original request.

Pre-request script.

Object.prototype.createObject = function(myPm){

    var counter = myPm.collectionVariables.get("obj_counter");
    myPm.collectionVariables.set(`obj_save_nb_${counter}`, `Hello there ${counter}`);
    counter++;
    myPm.collectionVariables.set("obj_counter", counter);     

}

Tests tab. (Counter set to 0 and using a loop)

pm.collectionVariables.set("obj_counter", 0);

for (let i = 0; i < 10; i++) {
    _.createObject(pm);
    console.log(`obj_save_nb_${i}: ` + pm.collectionVariables.get(`obj_save_nb_${i}`));
}`

image

image