Save endpoint variable through collection function

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 :wink: ) … I just want to understand why this is happening…


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

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:

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

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

response = pm.response.json();

fetchfirstarray=response[0];

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

My response-body looks like this:

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

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.

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…

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
1 Like

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! :partying_face:

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.

1 Like

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