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

**URL:** <https://community.postman.com/t/collection-variables-not-set-after-executing-collections-pre-request-script-without-errors/40741>\
**Category:** Help Hub\
**Tags:** variables, collections\
**Created:** [November 3, 2022, 2:08pm UTC](https://community.postman.com/t/collection-variables-not-set-after-executing-collections-pre-request-script-without-errors/40741 "2022-11-03T14:08:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pherrmann18](https://avatars.discourse-cdn.com/v4/letter/p/90db22/32.png) [@pherrmann18](https://community.postman.com/u/pherrmann18)\
**Post date:** [November 3, 2022, 2:08pm UTC](https://community.postman.com/t/collection-variables-not-set-after-executing-collections-pre-request-script-without-errors/40741/1 "2022-11-03T14:08:41Z")

</div>

_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:

 ![Screenshot_20221103_135354](https://us1.discourse-cdn.com/getpostman/original/3X/7/6/76f6d2c44ff2fefc29e36ebf34d6975ff8f71864.png)

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

 ![Screenshot_20221103_134703](https://us1.discourse-cdn.com/getpostman/original/3X/c/7/c752d411292dbd577ccbbf6b40e6545f41fdf48b.png)

this is the console output:  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/d/ad371bb4ea76f15da00d41d8fa72c049e3d5f4d9.png)

_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

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [November 3, 2022, 9:32pm UTC](https://community.postman.com/t/collection-variables-not-set-after-executing-collections-pre-request-script-without-errors/40741/3 "2022-11-03T21:32:43Z")

</div>

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](https://us1.discourse-cdn.com/getpostman/original/3X/5/3/5306f08086d7760e05befe499d3c4f15e0417fb3.png)

![image](https://us1.discourse-cdn.com/getpostman/original/3X/2/7/27174dc87b0b05acaab4ad2f64e27d8115f54eb6.png)

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

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/1/c1e02c6bacd363305bb4983fe13cc24a6c0a42c2.png)

---

<div class="post-metadata">

**Author:** ![pherrmann18](https://avatars.discourse-cdn.com/v4/letter/p/90db22/32.png) [@pherrmann18](https://community.postman.com/u/pherrmann18)\
**Post date:** [November 10, 2022, 8:37am UTC](https://community.postman.com/t/collection-variables-not-set-after-executing-collections-pre-request-script-without-errors/40741/4 "2022-11-10T08:37:42Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [November 10, 2022, 11:33am UTC](https://community.postman.com/t/collection-variables-not-set-after-executing-collections-pre-request-script-without-errors/40741/6 "2022-11-10T11:33:19Z")

</div>

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.

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

```

Tests tab

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

```

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [November 10, 2022, 11:56am UTC](https://community.postman.com/t/collection-variables-not-set-after-executing-collections-pre-request-script-without-errors/40741/7 "2022-11-10T11:56:11Z")

</div>

@pherrmann18

This also works.

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

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

```

Tests tab

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

```

To extend this.

The following will allow you to set a collectionVariable.

Pre-request script

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

```

Tests tab

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

```

It does seem that Object.prototype can work.

Back to your original request.

Pre-request script.

```auto
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)

```auto
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](https://us1.discourse-cdn.com/getpostman/original/3X/e/e/ee5d975900121437e52cd9665969037cc46ce8a0.png)

![image](https://us1.discourse-cdn.com/getpostman/original/3X/0/3/03b079273ebf71730db67f1432b73f2cc41c41c2.png)
