# Setting Collection variable programmatically

**URL:** <https://community.postman.com/t/setting-collection-variable-programmatically/9268>\
**Category:** Help Hub\
**Tags:** variables, collections, pre-request-script\
**Created:** [November 27, 2019, 12:52am UTC](https://community.postman.com/t/setting-collection-variable-programmatically/9268 "2019-11-27T00:52:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![conigbinde](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@conigbinde](https://community.postman.com/u/conigbinde)\
**Post date:** [November 27, 2019, 12:52am UTC](https://community.postman.com/t/setting-collection-variable-programmatically/9268/1 "2019-11-27T00:52:28Z")

</div>

I tried using the `pm.collectionVariables.set()` method to set a collection variable that shares the same variable name with an environment variable from my pre-request script.

The pre-request script runs successfully, but I realize that the value stored in the collection variable is the value of the variable in the environment scope, instead of taking on the new value from the pre-request script.

Is this the way it should work?

What do I have to do to ensure that the value computed in the pre-request script is stored in the collection variable?

---

<div class="post-metadata">

**Author:** ![suesmith](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/suesmith/32/2307_2.png) [@suesmith](https://community.postman.com/u/suesmith)\
**Post date:** [November 28, 2019, 7:20pm UTC](https://community.postman.com/t/setting-collection-variable-programmatically/9268/2 "2019-11-28T19:20:20Z")

</div>

Hi there, using `collectionVariables.set` should set the collection variable, I just tried that and it worked (I had an environment variable with the same name which remained unchanged). Could you either include the relevant parts of your code or walk through what else is happening with the variables when your requests run? Am wondering if the variables are being set somewhere else too…

---

<div class="post-metadata">

**Author:** ![conigbinde](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@conigbinde](https://community.postman.com/u/conigbinde)\
**Post date:** [November 29, 2019, 4:23am UTC](https://community.postman.com/t/setting-collection-variable-programmatically/9268/3 "2019-11-29T04:23:27Z")

</div>

Here is my code:

```auto
    var seed = Math.floor(Math.random() * 899999999999 + 100000000000);
    var d = new Date();
    var sessionId = '{{source_bank_code}}' + d.getFullYear().toString().substr(-2)+("0"+(d.getMonth()+1)).slice(-2)+("0" + d.getDate()).slice(-2)+("0" + d.getHours()).slice(-2) + ("0" + d.getMinutes()).slice(-2)+("0" + d.getSeconds()).slice(-2) + seed;

    var ben_acc_num = "1234567892";

    pm.collectionVariables.set("session_id", sessionId);
    pm.collectionVariables.set("beneficiary_account_number", ben_acc_num);
    console.log(sessionId);
    pm.collectionVariables.set("name_enquiry_ref", sessionId);

```

Attached is images showing what transpires in the collection and environment variable  
This is what we have in the collection variable before running the script:

 ![15](https://us1.discourse-cdn.com/getpostman/original/3X/6/7/670c8df01a1b455a19a0a37c8c5ef6b89f4c1fb2.png)

This is what we have in the collection variable after running the script which is obviously different from the value in the script:

 ![12](https://us1.discourse-cdn.com/getpostman/original/3X/4/1/41423f0f49c59c15fd7fcf8b558ef5b48720afa3.jpeg)  
This shows that the value assigned to the variable is taken from the current value of the variable in the environment scope instead of the value in the script as confirmed in the response body  
 ![42](https://us1.discourse-cdn.com/getpostman/original/3X/8/6/8606a667fb64d8978c91548010563b3cff33af7a.png)

The amazing thing is that whenever I set the environment to “No environment”, the script executes well and assigns the right value to the collection variable. Also, if I change the name of the variable to a name different from that which exists in the environment variable, the script runs just fine as well.
