# How to reuse a function

**URL:** <https://community.postman.com/t/how-to-reuse-a-function/39077>\
**Category:** Help Hub\
**Tags:** scripts, dry\
**Created:** [September 13, 2022, 10:25am UTC](https://community.postman.com/t/how-to-reuse-a-function/39077 "2022-09-13T10:25:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![idpbee](https://avatars.discourse-cdn.com/v4/letter/i/c37758/32.png) [@idpbee](https://community.postman.com/u/idpbee)\
**Post date:** [September 13, 2022, 10:25am UTC](https://community.postman.com/t/how-to-reuse-a-function/39077/1 "2022-09-13T10:25:12Z")

</div>

Is there a sane way to reuse a function?

I would like to put this in top root folder

```auto
function env(name) {
    return pm.environment.get(name);
}

```

and use it like that

> pm.expect(pm.response.json().client.id).to.eql(**env(‘clientId’)**);

what I don’t want is to write extra code like this in every test

```auto
const utils = eval(globals.loadUtils);
utils.validateResponseCode(200);

```

---

<div class="post-metadata">

**Author:** ![atchyutpulavarthi](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/atchyutpulavarthi/32/28229_2.png) [@atchyutpulavarthi](https://community.postman.com/u/atchyutpulavarthi)\
**Post date:** [September 17, 2022, 2:27pm UTC](https://community.postman.com/t/how-to-reuse-a-function/39077/2 "2022-09-17T14:27:07Z")

</div>

@idpbee you could do the following:

- In the Collection level pre-request script, declare an object without using the `var`, `let` or `const` keywords. The object is then attached to the global sandbox and is available in child context of that collection.

**In the collection pre-request script:**

```auto
utils = {
    getEnv(v) {
        return pm.environment.get(v);
    }
}

```

**Now within any of the children of that collection, you could simply do the following:**

```auto
utils.getEnv('name');

```

---

<div class="post-metadata">

**Author:** ![Adam](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@Adam](https://community.postman.com/u/Adam)\
**Post date:** [January 13, 2023, 6:21pm UTC](https://community.postman.com/t/how-to-reuse-a-function/39077/3 "2023-01-13T18:21:33Z")

</div>

Why it works only for getting variables? When in this same manner I have utils object with a method which changes **environment** Variables it doesn’t work. I need to pass explicitly in pre-request script of a request **pm** to make it work.

---

<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:** [January 25, 2023, 11:59am UTC](https://community.postman.com/t/how-to-reuse-a-function/39077/4 "2023-01-25T11:59:37Z")

</div>

@Adam

Just pass PM as an argument and this should work.

```auto
utils = {
    setEnv(myPm, envVar, val) {
        return myPm.environment.set(envVar, val);
    }
}

```

```auto
utils.setEnv(pm, 'testVariable', 'testValue');

```

---

<div class="post-metadata">

**Author:** ![p35](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/p35/32/15589_2.png) [@p35](https://community.postman.com/u/p35)\
**Post date:** [April 17, 2023, 10:18am UTC](https://community.postman.com/t/how-to-reuse-a-function/39077/5 "2023-04-17T10:18:03Z")

</div>

As an option, you can provide a util to set an actual `pm` for the current script.

And later, you can use it without passing your `pm on each call.

```auto
let _pm = null;
utils = {
    setPm(scriptPm) {
        _pm = scriptPm;
    },
    setEnv(envVar, val) {
        return _pm.environment.set(envVar, val);
    }
}

```

```auto
utils.setPm(pm);
utils.setEnv('testVariable', 'testValue');

```

It is especially useful when you are doing multiple utils calls where the actual `pm` is necessary.

```auto
utils.setPm(pm);
utils.setEnv('testVariable1', 'testValue1');
utils.setEnv('testVariable2', 'testValue2');
utils.setEnv('testVariable3', 'testValue3');

```

---

<div class="post-metadata">

**Author:** ![cardojp5](https://avatars.discourse-cdn.com/v4/letter/c/dfb087/32.png) [@cardojp5](https://community.postman.com/u/cardojp5)\
**Post date:** [January 26, 2024, 5:07pm UTC](https://community.postman.com/t/how-to-reuse-a-function/39077/6 "2024-01-26T17:07:19Z")

</div>

This seems not to work with newman ☹

utils is not handover from collection folder level to request level. Anyone have a solution that would work in newman ?

---

<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:** [January 26, 2024, 5:17pm UTC](https://community.postman.com/t/how-to-reuse-a-function/39077/7 "2024-01-26T17:17:48Z")

</div>

Hey @cardojp5 👋

Welcome to the Postman Community! :postman:

As this is an older topic now with a few different solutions suggested, could you share more contextual details about how you have things set up and an example of what’s not working so folks can help you further.
