# Setting Collection Variable doesn't work

**URL:** <https://community.postman.com/t/setting-collection-variable-doesnt-work/40305>\
**Category:** Help Hub\
**Created:** [October 21, 2022, 6:55am UTC](https://community.postman.com/t/setting-collection-variable-doesnt-work/40305 "2022-10-21T06:55:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dacreator](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@dacreator](https://community.postman.com/u/dacreator)\
**Post date:** [October 21, 2022, 6:55am UTC](https://community.postman.com/t/setting-collection-variable-doesnt-work/40305/1 "2022-10-21T06:55:58Z")

</div>

_My question_:  
I have 2 collections.  
In C1 I send a request to get the response value of “access\_type” and set its value in the Collection variable “CollRefToken”.  
This works fine. every time I POST the request, the current value of CollRefToken is being updated. The Code to set the CollVar is under “Tests” in the POST request.

In C2 I want to do the same. Same code, but here the Collection variable is not being updated. The request runs without error in both collections.

_Details (like screenshots)_:

**Collection 1 (CollRefToken is being updated)**

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/d/6d2624e423f63e3d3ebe81d154c88f0587580f6b.png)

```auto
let jsonResponse = pm.response.json();
console.log(jsonResponse);
pm.collectionVariables.set("CollRefToken", jsonResponse.access_token);

```

```auto
{
    "access_token": "VALUE FOR ColVar",
    "refresh_token": "xxx",
    "expires_in": 3600000,
    "token_type": "Bearer"
}

```

**Collection 2 (CollRefTokenDev is not being updated)**

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

```auto
let jsonResponse = pm.response.json();
console.log(jsonResponse);
pm.collectionVariables.set("CollRefTokenDev", jsonResponse.access_token);

```

RESPONSE

```auto
{
    "access_token": "VALUE FOR ColVar",
    "refresh_token": "xxx",
    "expires_in": 3600000,
    "token_type": "Bearer"
}

```

_How I found the problem_:  
No error message when sending the request, new access token can be seen in the response, but collection variable is unchanged.

_I’ve already tried_:  
renaming collection variable, deleting its initial value

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [October 21, 2022, 9:11am UTC](https://community.postman.com/t/setting-collection-variable-doesnt-work/40305/2 "2022-10-21T09:11:20Z")

</div>

Hi @dacreator

What’s in the Auth tab? … your first (working) screenshot has something in auth, but the second (failing) screenshot doesn’t.

---

<div class="post-metadata">

**Author:** ![dacreator](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@dacreator](https://community.postman.com/u/dacreator)\
**Post date:** [October 21, 2022, 9:21am UTC](https://community.postman.com/t/setting-collection-variable-doesnt-work/40305/3 "2022-10-21T09:21:48Z")

</div>

Hi [w4dd325](https://community.postman.com/u/w4dd325),  
correct, I had a look, just an old variable reference which is unused. I deleted this, shouldn’t have an effect.

I think I had some weird spelling mistake in the code, as it is working now.  
Only thing I did was close Postman and try again.  
Using the same code as before as far I can see.

```auto
let jsonResponse = pm.response.json();
console.log(jsonResponse);
pm.collectionVariables.set("CollRefTokenDev", jsonResponse.access_token);

```

Thanks for your help.
