# Global function issue

**URL:** <https://community.postman.com/t/global-function-issue/13435>\
**Category:** Help Hub\
**Tags:** newman-project, tests, collections\
**Created:** [June 15, 2020, 9:57pm UTC](https://community.postman.com/t/global-function-issue/13435 "2020-06-15T21:57:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjayaram](https://avatars.discourse-cdn.com/v4/letter/r/3d9bf3/32.png) [@rjayaram](https://community.postman.com/u/rjayaram)\
**Post date:** [June 15, 2020, 9:57pm UTC](https://community.postman.com/t/global-function-issue/13435/1 "2020-06-15T21:57:57Z")

</div>

Team

I am exploring global functions. A function that has to be called by many requests in the same collection as well by many requests in the same tests.

For example, I have 10 requests in the collection. Each request when executed, returns huge JSon response. I am parsing the response and taking only the image link from the response. I need to validate if the image link is working or not.

Appreciate your input on the following issue and looking for efficient way to handle this.

1. As I have to validate image link (to make sure the link is reachable or not) from all of the 10 requests, I thought of putting the link validation function in the pre-request script as a global function.
2. In the test, I am calling the global function
3. It is not evaluating the function correctly
4. Can I define the function once and call it from each test in the collection (sort of folder level or collection level function) ?
5. OR do I need to repeat the same global function in each request’s pre-req tab?

## Pre-req script

postman.setGlobalVariable(‘linkcheck\_Function’, (link)=\> {  
pm.sendRequest(link, function(err, resp) {  
console.log("In the function status= "+resp.code);  
//This is printing the status code correctly inside the function  
return resp.code;  
});  
});

## In the test tab of the same request

var imagelink\_check\_status="";  
//—code block here to process and parse the response and saving the link in the imageurl------  
console.log("image url = "+imageurl);  
imagelink\_check\_status=eval(globals.linkcheck\_Function)(imageurl);  
console.log("image link and check status = "+imageurl + " " +imagelink\_check\_status);  
//imageurl is printed correctly on the console but the imagelink\_check\_status is undefined

Results  
image url is printed correctly but the status is “undefined”

---

<div class="post-metadata">

**Author:** ![jetison](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jetison/32/12_2.png) [@jetison](https://community.postman.com/u/jetison)\
**Post date:** [June 15, 2020, 10:24pm UTC](https://community.postman.com/t/global-function-issue/13435/2 "2020-06-15T22:24:09Z")

</div>

Hi there @rjayaram -

Looks like this question has been asked already ([Global functions via Collection level folder?](https://community.postman.com/t/global-functions-via-collection-level-folder/5927) , [Where is the best place to set global functions for tests?](https://community.postman.com/t/where-is-the-best-place-to-set-global-functions-for-tests/7440)). Can you see if any of these threads answer your questions? Or weigh in on either thread?

---

<div class="post-metadata">

**Author:** ![rjayaram](https://avatars.discourse-cdn.com/v4/letter/r/3d9bf3/32.png) [@rjayaram](https://community.postman.com/u/rjayaram)\
**Post date:** [June 15, 2020, 10:31pm UTC](https://community.postman.com/t/global-function-issue/13435/3 "2020-06-15T22:31:29Z")

</div>

Hi Joyce

Thanks for looking into this issue. I have looked into the thread that you referenced. As suggested in the reply I was trying to define a global function and evaluating the value of the function in the test. The only difference is, I need to call this function from my test many times. As stated in my problem earlier, the value of the function is not evaluated correctly. Pl see my code snippet from test tab and pre-req script.

Thanks, Raje J

---

<div class="post-metadata">

**Author:** ![jetison](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jetison/32/12_2.png) [@jetison](https://community.postman.com/u/jetison)\
**Post date:** [June 15, 2020, 11:01pm UTC](https://community.postman.com/t/global-function-issue/13435/4 "2020-06-15T23:01:49Z")

</div>

> imagelink\_check\_status=eval(globals.linkcheck\_Function)(imageurl);

Not sure you’re passing the parameter correctly. Does it work if you update the above line of code to:

```auto
let imagelink_check_status = eval(globals.linkcheck_Function)
imagelink_check_status(imageurl)

```

---

<div class="post-metadata">

**Author:** ![rjayaram](https://avatars.discourse-cdn.com/v4/letter/r/3d9bf3/32.png) [@rjayaram](https://community.postman.com/u/rjayaram)\
**Post date:** [June 16, 2020, 1:53am UTC](https://community.postman.com/t/global-function-issue/13435/5 "2020-06-16T01:53:42Z")

</div>

Hi Joyce

Thanks for your suggestion. I have updated as below. Looks like that function is now getting executed. I see **In the function status= OK** in the console. However the env variable “url\_reachable” is always set to false. Looks like it did not understand unset command at the end.  
let me know if I am missing something.

Thanks  
**Pre-req script**

_postman.setGlobalVariable(‘linkcheck\_Function’, (link)=\> {_

- pm.sendRequest(link, function(err, resp) {\*
- 

```
  // pm.expect(err).to.not.be.ok;*

```

- console.log("In the function status= "+resp.status);\*
- return resp.status;\*
- });\*  
_})_

**Test tab (updated per your suggestion)**  
_let imagelink\_check\_status=eval(globals.linkcheck\_Function)_

- 

```
       imagelink_check_status(imageurl);*

```

- 

```
       console.log("image link and check status = "+imageurl + " " +imagelink_check_status);*

```

- 

```
       pm.environment.set("url_reachable",false);*

```

At the end of the test, I have:  
**pm.environment.unset(“url\_reachable”);**

---

<div class="post-metadata">

**Author:** ![vdespa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vdespa/32/2770_2.png) [@vdespa](https://community.postman.com/u/vdespa)\
**Post date:** [June 16, 2020, 2:24am UTC](https://community.postman.com/t/global-function-issue/13435/6 "2020-06-16T02:24:37Z")

</div>

@rjayaram pm.sendRequest is an asynchronous function, you can’t simply return a value from there.

Consider writing your test inside the pm.sendRequest function. Example:

```
pm.sendRequest('https://postman-echo.com/get', (error, response) => {
    if (error) { 
      console.log(error); 
    }

  pm.test('response should be okay to process', () => {
    pm.expect(error).to.equal(null);
    pm.expect(response).to.have.property('code', 200);
    pm.expect(response).to.have.property('status', 'OK');
  });
});

```

Many users get confused about scripts and eval. You can do this much cleaner by using a normal postman request where you pass the URL as a variable.

---

<div class="post-metadata">

**Author:** ![rjayaram](https://avatars.discourse-cdn.com/v4/letter/r/3d9bf3/32.png) [@rjayaram](https://community.postman.com/u/rjayaram)\
**Post date:** [June 16, 2020, 3:39am UTC](https://community.postman.com/t/global-function-issue/13435/7 "2020-06-16T03:39:05Z")

</div>

Hi Vdespa

Thanks for looking into this. I knew it is a asynchronous function. The reason why I prefer a global function is, I have say 10 requests in the collection. Each request has many tests. Each response gives “N” number of urls. Hence instead of repeating **pm.sendRequest** N times, I would like to declare that as a function and call it in the loop when iterating over the response array. Let me know efficient and simple solution to achieve this. All I need to do is validate the dynamic urls received from each request. If the url is not valid, then I need to write it in the console log and update the env variable /flag.
