# Cannot set const globalFunction = eval(globals.globalFunction)(); in collection prereq

**URL:** <https://community.postman.com/t/cannot-set-const-globalfunction-eval-globals-globalfunction-in-collection-prereq/13509>\
**Category:** Help Hub\
**Created:** [June 18, 2020, 3:40pm UTC](https://community.postman.com/t/cannot-set-const-globalfunction-eval-globals-globalfunction-in-collection-prereq/13509 "2020-06-18T15:40:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasoncaunt](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jasoncaunt/32/4763_2.png) [@jasoncaunt](https://community.postman.com/u/jasoncaunt)\
**Post date:** [June 18, 2020, 3:40pm UTC](https://community.postman.com/t/cannot-set-const-globalfunction-eval-globals-globalfunction-in-collection-prereq/13509/1 "2020-06-18T15:40:22Z")

</div>

I am using a global env file to have some functions in.  
To call those functions in my test I use

```
const globalFunction = eval(globals.globalFunction)();
globalFunction.execute.GetAccountNumber();

```

Now, every request has to have the  
`const globalFunction = eval(globals.globalFunction)();` so I can use my function. Rather than having to add this in each request. I am trying to call it in the collections prereq script. So called before each test.

However, it doesn’t seem to work.  
I get the error  
There was an error in evaluating the Pre-request Script: ReferenceError: globalFunction is not defined

What is my problem??

---

<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:** [June 18, 2020, 9:52pm UTC](https://community.postman.com/t/cannot-set-const-globalfunction-eval-globals-globalfunction-in-collection-prereq/13509/2 "2020-06-18T21:52:18Z")

</div>

Hey @jasoncaunt

Welcome to the Postman community! 🚀

Would you able able to share more about the function itself and maybe a screenshot of the different areas that you have that in the app?

Just trying to visualize what you’re doing 😁

Also, would getting the function this way help?

```
eval(pm.globals.get("globalFunction"))

```

This thread might add some extra information into getting something working:

> [@Global functions via Collection level folder?](https://community.postman.com/t/global-functions-via-collection-level-folder/5927):
>
> I am very sorry if this has already been covered. I have found conflicting information online and just want to know if this is an actual available feature or not. I want to know if it’s possible to have global functions at the Collection level folder (either in the pre-request or tests section) where I can store any functions I use for nearly every test. For example say for every request I do the following: console.log(‘hello!’); could I instead create a function in say the pre-request secti…

---

<div class="post-metadata">

**Author:** ![jasoncaunt](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jasoncaunt/32/4763_2.png) [@jasoncaunt](https://community.postman.com/u/jasoncaunt)\
**Post date:** [June 23, 2020, 8:33am UTC](https://community.postman.com/t/cannot-set-const-globalfunction-eval-globals-globalfunction-in-collection-prereq/13509/3 "2020-06-23T08:33:36Z")

</div>

My function looks like

```
    var CheckResponseCount = (retryDelay, retryLimit, expectectedCount, retryCount = 1) => {
    var bearer_token = pm.environment.get("bearer_token");

    // Construct our request URL from environment variables
    var baseurl = request["url"].replace("{{BaseURL}}", pm.environment.get("BaseURL"));
    baseurl = baseurl.replace("{{AccountId}}", pm.environment.get("AccountId"));
    pm.sendRequest({
        url: baseurl,
        method: 'GET',
        header: {
            'content-type': 'application/json',
            'authorization': 'Bearer ' + bearer_token
        }
    }, function (err, response) {
        if (err) {
            console.log("ERROR:" + err);
    } else {
            if (response.json().length !== expectectedCount) {
                if (retryCount < retryLimit) {
                    console.log('Job is still PENDING. Retrying in ' + retryDelay + 'ms');
                    setTimeout(function () {
                        CheckResponseCount(retryDelay, retryLimit, expectectedCount, ++retryCount);
                    }, retryDelay);
                } else {
                    console.log('Retry limit reached, giving up.');
                    pm.test(pm.info.requestName + " - FAILED TO GET CORRECT RESPONSE COUNT", function () {
                        pm.expect(response.json().length).to.eql(!expectectedCount)
                    });
                    postman.setNextRequest(null);
                }
            }
            if (response.json().length === expectectedCount) {
                pm.test(pm.info.requestName + " - Correct Response Count", function () {
                        pm.expect(response.json().length).to.eql(expectectedCount)
                });
            }
        }
    });
}

```

I currently call this function in my pre-req of my request

```
const globalFunction = eval(globals.globalFunction)();
globalFunction.execute.CheckResponseCount(5000,15,1);

```

But instead of having the  
`const globalFunction = eval(globals.globalFunction)();`  
in every request in the collection. I want it just once in the Collections Pre-Req section

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