Setting/getting collection variable value in script

I have a collection variable defined (Collection > Edit > Variables tab. Variable name: token) that Iā€™m using in as request header value. I want to set its value in script. Iā€™m trying to set it with this:

pm.variables.set(ā€œtokenā€, jsonData.access_token);

but the Collection variable value doesnā€™t change. In the past Iā€™ve used pm.globals and pm.environment, and those work here as expected, but this variable is actually scoped to the collection. Iā€™d rather work with it within that scope than use the broader globals or environment scope.

1 Like

@CBoland First of all, welcome to the community, thanks for coming to ask a good question like this.
When it comes to Collection Variables they are treated a bit differently than Environment and Global variable.
https://learning.getpostman.com/docs/postman/environments_and_globals/variables#defining-collection-variables

Hopefully this will help you out a bit. If the variable is not static, and I assume thats the case if its a token. You will likely need to scope this as an environment variable, you can also unset it at the end of the tests if need be.

Hopefully this helps.

1 Like

Yes, @tmccann, that helps. It appears then that Collection variables are static whereas Environment and Global variables are dynamic. Perhaps the documentation could be clarified to this effect? Another option is to make Collection variables dynamic (and consistent) with Environment and Globals.

2 Likes

Let me renew this topic, I do love Postman but it comes with lot of frustration about this variables management.

I maintain collections in my workspace, with my environment, I define and then update variables on pre-scripts and tests :

pm.environment.set("my_token_id", jsonData.uid);

But when it comes to sharing my collections, if the receiver donā€™t have any environement, then no variable is set. So I ended setting the environment AND the global variables :

pm.globals.set("my_token_id", jsonData.uid);
pm.environment.set("my_token_id", jsonData.uid);

But as collections are the way to easily export and share your API or a special workflow, it would be so much elegant to handle all variables into this collection.

How to request for this update ?

2 Likes

Hey @julienandre,

Welcome to the community! :wave:

The ability to programmatically get and set variables, at the collection level, has been part of the app for a while now. Hopefully, this new functionality will help solve some of the issues youā€™re facing.

To use them in the app (Pre-request Script and Tests sandboxes), itā€™s just a case of using the collectionVariables method on the pm.* API. This new method works in the same way as you would use the get, set, unset and clear functions on the variables, at the Environment and Global level.

pm.collectionVariables.set('my_var', 'my_value')  

pm.collectionVariables.get('my_var')

pm.collectionVariables.unset('my_var')

pm.collectionVariables.clear()

More information can be found on the Learning Center:

https://learning.getpostman.com/docs/postman/scripts/postman-sandbox-api-reference/#pmcollectionvariables

4 Likes

Wow, thatā€™s perfect !
Sorry I missed this section into the doc !

Youā€™re very welcome!

Weā€™re in the process of updating the documentation in the Learning Center so hopefully, this will become a lot clearer soon.

The Learning Center is open source and weā€™re always looking for willing folks to help improve the content for other users - If youā€™re up for contributing, that would be awesome. :trophy:

Screenshot 2020-01-15 at 11.04.22

At the top of each page, you will find this button, this will link to the repo! :slight_smile:

Hello,

I am trying to set an environment variable for a particular request but after the request is run, the value is not assigned to the variable even though the variable is created from that script, the values are empty. Please advise.

1 Like

Hey @tifedada,

Are you able to provide an example of what youā€™re seeing, please?

An image would be good :slight_smile: - Maybe also add the code you have in the script, the more information the better :slight_smile:

1 Like

Hey @danny-dainton

I figured out the problem while I was getting the information to send to you together. The response for this particular request is in an array and I didnā€™t cater for that in the script I wrote to set the variable. Thank you.

1 Like

I am using version 7.17.0 on the mac. The pm.collectionVariables.set() simply does not work. Just try something as easy as
pm.collectionVariables.set(ā€œmy_varā€, 'some-value);
console.log("my_var = " +pm.collectionVariables.get(ā€œmy_varā€));

and you will get an undefined.

1 Like

I am using Postman v7.30.1. In test script I wrote:
I want to extract the access_token from the JSON response and store it in Collections Variable:

var jsonData = pm.response.json();
pm.collectionVariables.set(ā€˜access_tokenā€™, jsonData.access_token);

Then i tried setting collection variable to a hard coded value, but that too does not work:

pm.collectionVariables.set(ā€˜access_tokenā€™, ā€˜abcā€™);

Am I missing something?

Was excited to see this but I could not get this to work with Postman 7.8.0 on the Mac. Trying to use the set method I get:

TypeError: Cannot read property ā€˜setā€™ of undefined

I think others have mentioned this as well. Is there some fine detail weā€™re overlooking here?

Hey @trinigooner

Welcome to the Postman community! :rocket:

That feature wasnā€™t introduced till v7.9.0

https://www.postman.com/downloads/release-notes/

You would need to update the app version to see this.

Hey @sagarkm

What does your response body look like?

Did this set a blank variable at the collection level?

Select the 3 dots on the Collection name and hit Edit to see the Collection level elements.

Screenshot 2020-08-18 at 16.11.52

@sagarkm

This seemed to work for me.

In the collections tab on the left panel , firstly click on the 3 dots and choose edit.
After doing so, already set your variable name as access_token.
Then in the test script, I would always suggest to place the following code: this helps to see the status every time:

pm.test('Status code is 200', function () {
    pm.response.to.have.status(200); 
});

After doing so paste this code:

var jsonBody = pm.response.json();

pm.collectionVariables.set("access_token",jsonBody.access_token);

If I define a collection variable, neither the current value nor the initial value seems to be appliedā€¦

Hi all! I know this topic still sees a good bit of action, so I wanted to provide a hands-on option for a solution!

Check out this collection in the Postman Answers public workspace. You can fork it to test it out yourself!

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