# Save endpoint variable through collection function

**URL:** <https://community.postman.com/t/save-endpoint-variable-through-collection-function/64806>\
**Category:** Help Hub\
**Tags:** tests, variables, collections, javascript, functions\
**Created:** [July 31, 2024, 9:34am UTC](https://community.postman.com/t/save-endpoint-variable-through-collection-function/64806 "2024-07-31T09:34:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joint-operations-ob5](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/joint-operations-ob5/32/29583_2.png) [@joint-operations-ob5](https://community.postman.com/u/joint-operations-ob5)\
**Post date:** [July 31, 2024, 9:34am UTC](https://community.postman.com/t/save-endpoint-variable-through-collection-function/64806/1 "2024-07-31T09:34:04Z")

</div>

**Intro** : Hi there, I’m on a beginner level of javascript/postman-scripts and trying to figure some stuff out myself to obtain knowledge in this area. I’m using an existing API to figure things out.

**My goal** : I want to use a function as a pre-request script on a collection-level which I can use inside different post-response test-scripts that are on an endpoint-level.

**What’s working** : The **Scenario#1** is working. When I smash everything in one and the same post-response script on the endpoint-level, the expected value (“111-A1”) gets saved as an environment variable.

**What’s not working(/my goal)**: In stead of having to put this function on every endpoint, I want to put this function on the collection-level and just use the set function-name inside each endpoint-level script, So, the combination of **Scenario#2-Part1** and **Scenario#2-Part2** are _not working_.

**So; my question** : I don’t understand why this is not working. I’ve compared it to several pieces of javascript code, asked my digital robot-friend (famous AI), et cetera and yet I’m creating this thread, because I can’t figure it out. I’m leaning towards “somehow Postman doesn’t allow for a collection-level function” and that’s why I’m creating this thread in this community. Does anyone know if this, in fact, is the case and/or if there is another solution?

**Extra information** : _I’ve tried about 4 variations of basically the same code, but without any luck and thus I’m looking for somebody that actually understands why I can’t seem to achieve the stated goal in stead of just posting the 5th variation (no hard feelings, of course 😉 ) … I just want to understand why this is happening…_

* * *

**Scenario#1 - My post-response script on an endpoint-level** :

```auto
utils = { 
    setEnvironmentVariable: function(key, value) {
        
        if (value) {
            
            pm.environment.set(key, value);
        }
    }
};

response = pm.response.json();

fetchfirstarray=response[0];

if ('object1' in fetchfirstarray) {
    utils.setEnvironmentVariable("object1variable", fetchfirstarray.object1);
    }

```

* * *

**Scenario#2-Part1 - My pre-request script on a collection-level** :

```auto
utils = { 
    setEnvironmentVariable: function(key, value) {
        
        if (value) {
            
            pm.environment.set(key, value);
        }
    }
};

```

**Scenario#2-Part2 - My post-response script on an endpoint-level** :

```auto
response = pm.response.json();

fetchfirstarray=response[0];

if ('object1' in fetchfirstarray) {
    utils.setGlobalVariable("object1variable", fetchfirstarray.object1);
    }

```

* * *

**My response-body looks like this** :

```auto
[
    {
        "object1": "111-A1",
        "object2": "222-A1",
        "object3": "333-A1"
    },
    {
        "object1": "111-A2",
        "object2": "222-A2",
        "object3": "333-A2"
    },
]

```

---

<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:** [July 31, 2024, 10:20am UTC](https://community.postman.com/t/save-endpoint-variable-through-collection-function/64806/2 "2024-07-31T10:20:22Z")

</div>

My first observation is that the function is called “setEnvironmentVariable” yet you are calling setGlobalVariable.

If you want to use Postman functions, you have to pass the Postman method into the request. It works in your first example as the pm methods are available to all of the code in a post request script, but is not available in a pre-request script, so you have to pass in the pm object to get access to those methods.

The function should be declared in the pre-request script.

```auto
utils = {
    statusCode: function (pm, code) {
        pm.test(`Status code is ${code}`, () => {
            pm.response.to.have.status(code);
        })
        return utils;
    },
    response: function (pm) {
        console.log(pm.response.json());
        return utils;
    },
    setEnvironmentVariable: function (pm, key, value) {
        if (value) {
            pm.environment.set(key, value);
            return utils;
        }
    }
};

```

Then called via…

```auto
utils.response(pm);
utils.statusCode(pm, 200);

response = pm.response.json();
fetchfirstarray=response[0];

if ('object1' in fetchfirstarray) {
    utils.setEnvironmentVariable(pm, "object1variable", fetchfirstarray.object1);
};

console.log(pm.environment.get("object1variable")); // 111-A1

```

---

<div class="post-metadata">

**Author:** ![joint-operations-ob5](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/joint-operations-ob5/32/29583_2.png) [@joint-operations-ob5](https://community.postman.com/u/joint-operations-ob5)\
**Post date:** [July 31, 2024, 11:41am UTC](https://community.postman.com/t/save-endpoint-variable-through-collection-function/64806/3 "2024-07-31T11:41:25Z")

</div>

Mike, you are my personal hero of the day! Your first observation was correct, I made a typo in the dummy date I wanted to share in this post in stead of the actual data and I turned out to be the data-dummy!

It turns out I was missing the ‘pm’ part. I just learned that this method needs to be defined as this is, as it turns out, the missing bridge (method) between the collection and the endpoint -levels.

Thank you so much for your reply and helping my out one becoming a tad bit more knowledgeable! 🥳

Edit: _another, smaller, ‘thank you’ for adding a couple of valuable tests… I can totally tweak those a little and add 'em to the ones I already have._

---

<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:** [August 3, 2024, 11:42am UTC](https://community.postman.com/t/save-endpoint-variable-through-collection-function/64806/4 "2024-08-03T11:42:02Z")

</div>

Closed on August 3, 2024, 11:42am UTC

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