# Setting a variable value from a collection level script

**URL:** <https://community.postman.com/t/setting-a-variable-value-from-a-collection-level-script/74822>\
**Category:** Help Hub\
**Tags:** scripts\
**Created:** [February 3, 2025, 2:43pm UTC](https://community.postman.com/t/setting-a-variable-value-from-a-collection-level-script/74822 "2025-02-03T14:43:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tim-cartwright](https://avatars.discourse-cdn.com/v4/letter/t/47e85d/32.png) [@tim-cartwright](https://community.postman.com/u/tim-cartwright)\
**Post date:** [February 3, 2025, 2:43pm UTC](https://community.postman.com/t/setting-a-variable-value-from-a-collection-level-script/74822/1 "2025-02-03T14:43:27Z")

</div>

How do I set a variable from a postman script at the collection level, or from a package script? Using the postman app.

The

```auto
pm.environment.set('FOO', 'BAR-ENV');
pm.globals.set('FOO', 'BAR-GLOBALS');

```

Adding this to a collection level script has no impact, and the values do not show up anywhere? Is this possible? Or is there any work around? Currently right now I have to return the data to the request script and then set the variable.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [February 3, 2025, 2:51pm UTC](https://community.postman.com/t/setting-a-variable-value-from-a-collection-level-script/74822/2 "2025-02-03T14:51:44Z")

</div>

Hey @tim-cartwright 👋

Welcome to the Postman Community! :postman:

You would need to use the `pm.collectionVariables.set()` syntax for those to go to the Collection scoped variable section.

> **[Process data and script workflows using Postman JavaScript objects | Postman...](https://learning.postman.com/docs/tests-and-scripts/write-scripts/postman-sandbox-api-reference/#use-collection-variables-in-scripts)**
>
> Postman is a collaboration platform for API development. Postman's features simplify each step of building an API and streamline collaboration so you can create better APIs—faster.

* * *

For your script, if you don’t have an active environment set, that wouldn’t go anywhere as it wouldn’t have anywhere to “store” it.

For the Global variables - You could see them by either opening the `All variables` menu from the top right icon:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/8/b/8be686d4460bf24901d4f7970c8a97708da9c105.png)

Or opening the Environments section from the let sidebar and selecting `Globals`

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/9/8/98651663b69113fb347426c6c5849c8421b071b2.png)

---

<div class="post-metadata">

**Author:** ![tim-cartwright](https://avatars.discourse-cdn.com/v4/letter/t/47e85d/32.png) [@tim-cartwright](https://community.postman.com/u/tim-cartwright)\
**Post date:** [February 4, 2025, 4:33pm UTC](https://community.postman.com/t/setting-a-variable-value-from-a-collection-level-script/74822/3 "2025-02-04T16:33:13Z")

</div>

I do have an environment set. It just does not work. No error. No value change. Nothing. I tried pm.collectionVariables.set with the same result.

So apparently this issue only arises when the variable is set from a function created at the collection level. If the script sets the variables outside of a function it works.

I have created an example repro workspace:

[https://www.postman.com/svapi4/testing/request/40551444-f2662dca-35ac-459d-b7e5-32dba3e0f605?tab=body](https://www.postman.com/svapi4/testing/request/40551444-f2662dca-35ac-459d-b7e5-32dba3e0f605?tab=body)

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [February 4, 2025, 5:39pm UTC](https://community.postman.com/t/setting-a-variable-value-from-a-collection-level-script/74822/4 "2025-02-04T17:39:05Z")

</div>

Hey @tim-cartwright,

Your original question is missing a lot of contextual detail that’s actually quite relevant here. You didn’t mention that you had created a _global_ utility function and you were creating variables within that, by calling it in the request script.

Those work slightly differently and is an older method of reusing functions within scripts.

In order to get your function working with the `pm.*` functions, you need to add `pm` as an argument:

```auto
SetInfo: function(pm)

```

This also needs to be in the function at the script level:

```auto
utils.SetInfo(pm);

```

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/2/62f615e7eac152a5b3569e5e3f0d1ae6dcfc3890.png)

I would recommend taking a look at the Package Library feature.

People still use those global functions and have no issues but the Package Library allows the whole team to use your scripts within any Workspace.

> **[Reuse scripts in Postman | Postman Docs](https://learning.postman.com/docs/tests-and-scripts/write-scripts/package-library/)**
>
> Postman is a collaboration platform for API development. Postman's features simplify each step of building an API and streamline collaboration so you can create better APIs—faster.

---

<div class="post-metadata">

**Author:** ![tim-cartwright](https://avatars.discourse-cdn.com/v4/letter/t/47e85d/32.png) [@tim-cartwright](https://community.postman.com/u/tim-cartwright)\
**Post date:** [March 28, 2025, 2:04pm UTC](https://community.postman.com/t/setting-a-variable-value-from-a-collection-level-script/74822/5 "2025-03-28T14:04:07Z")

</div>

> [@danny-dainton](#):
>
> `pm`

Thanks, I will try passing in pm to the scripts.
