# Creating functions and reuse in other test scripts

**URL:** <https://community.postman.com/t/creating-functions-and-reuse-in-other-test-scripts/60467>\
**Category:** Help Hub\
**Tags:** functions\
**Created:** [April 29, 2024, 8:22pm UTC](https://community.postman.com/t/creating-functions-and-reuse-in-other-test-scripts/60467 "2024-04-29T20:22:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![docking-module-geol7](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@docking-module-geol7](https://community.postman.com/u/docking-module-geol7)\
**Post date:** [April 29, 2024, 8:22pm UTC](https://community.postman.com/t/creating-functions-and-reuse-in-other-test-scripts/60467/1 "2024-04-29T20:22:58Z")

</div>

I have searched for the solution and found multiple answers but didn’t work for me.

Under Collection Folder/Parent folder → Under Pre-request script ..  
[Note - env\_amount is created under Collection Folder → Variables tab]

function getAmount()  
{ const amount= (“random\_number”, \_.random(1, 10));  
pm.environment.set(“env\_amount”,amount);  
};

In the later folders/ tests - need to get this amount in ‘Body’ request. So I have added the following stmt to Pre-request of that test:  
var amount=pm.collectionVariables.get(“getAmount”)();

In the body of the test -  
“amount”: {{amount}},

its not working.

---

<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:** [April 30, 2024, 11:00am UTC](https://community.postman.com/t/creating-functions-and-reuse-in-other-test-scripts/60467/2 "2024-04-30T11:00:29Z")

</div>

Your function is trying to set an environment variable, where in the pre-request script you have it trying to retrieve a collection variable that doesn’t appear to be declared anywhere.

The best way to declare a function is to define it as a global variable (don’t use LET, VAR or CONST)

```auto
utils = {
    getAmount: function (pm) {
        const amount = ("random_number", _.random(1, 10));
        pm.environment.set("env_amount", amount);
        return 'hello';
    },
    response: function (pm) {
        console.log(pm.response.json());
        return 'hello';
    }
};

```

If you need access to Postman functions like pm.response or pm.environment, then you have to pass pm as an argument to the function.

Calling the function is then straight forward.

```auto
utils.getAmount(pm); //or
utils.response(pm);

```

All you should need for your body is the following.

```auto
{
    "amount": {{env_amount}}
}

```

This will use the environment variable you set in the function.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [May 30, 2024, 11:00am UTC](https://community.postman.com/t/creating-functions-and-reuse-in-other-test-scripts/60467/3 "2024-05-30T11:00:57Z")

</div>

Closed on May 30, 2024, 11:00am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
