# Collection variables not updating via pre-script

**URL:** <https://community.postman.com/t/collection-variables-not-updating-via-pre-script/76716>\
**Category:** Help Hub\
**Tags:** variables, collections, scripts, pmsendrequest\
**Created:** [March 11, 2025, 8:42am UTC](https://community.postman.com/t/collection-variables-not-updating-via-pre-script/76716 "2025-03-11T08:42:52Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![devteamadipsys](https://avatars.discourse-cdn.com/v4/letter/d/65b543/32.png) [@devteamadipsys](https://community.postman.com/u/devteamadipsys)\
**Post date:** [March 11, 2025, 8:42am UTC](https://community.postman.com/t/collection-variables-not-updating-via-pre-script/76716/1 "2025-03-11T08:42:52Z")

</div>

Hi here !

Yesterday i have write a pre-script for my collection.  
I had used :  
pm.collectionVariables.set(‘sessionId’, response.json().sessionId);

which worked great but this morning (update v11.36.1), it does not working anymore.

The collection variables are not created/updated by the pre-script (current value)

Is there a bug or something ?

Thanks

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 11, 2025, 8:54am UTC](https://community.postman.com/t/collection-variables-not-updating-via-pre-script/76716/2 "2025-03-11T08:54:11Z")

</div>

Hey @devteamadipsys 👋🏼

Welcome to the Postman Community :postman:

> pm.collectionVariables.set(‘sessionId’, response.json().sessionId);

This is missing the `pm.` at the start of `response.json()`. That could be the issue but we also don’t know what else you have in the script or what the structure of that response is.

Are you able to provide more visual examples of this issue?

---

<div class="post-metadata">

**Author:** ![devteamadipsys](https://avatars.discourse-cdn.com/v4/letter/d/65b543/32.png) [@devteamadipsys](https://community.postman.com/u/devteamadipsys)\
**Post date:** [March 11, 2025, 8:58am UTC](https://community.postman.com/t/collection-variables-not-updating-via-pre-script/76716/3 "2025-03-11T08:58:40Z")

</div>

The script :

```auto
pm.sendRequest(getSessionId, (error, response) => {
        if (error) {
            console.log(error);
        }

        pm.test('get sessionId', async () => {
            pm.expect(error).to.equal(null);
            pm.expect(response).to.have.property('code', 200);
            pm.expect(response).to.have.property('status', 'OK');
            console.log(response.json());
            pm.collectionVariables.set('sessionId', 
            response.json().sessionId);
     })
})

```

the pm seems not be require since the console.log reponse.json() give me the correct result.

I’ve seen that other variables still be updated just this one seems not.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 11, 2025, 9:06am UTC](https://community.postman.com/t/collection-variables-not-updating-via-pre-script/76716/4 "2025-03-11T09:06:15Z")

</div>

The fact that it’s inside an async request was quite vital information when you’re asking people to help you. That changes the context of ‘response’ here.🙏🏻

What’s the console output? Where are you seeing that the Collection Variable is not updated? Have you tried changing the scope of that variable to see it it can be set at the global or environment level?

---

<div class="post-metadata">

**Author:** ![devteamadipsys](https://avatars.discourse-cdn.com/v4/letter/d/65b543/32.png) [@devteamadipsys](https://community.postman.com/u/devteamadipsys)\
**Post date:** [March 11, 2025, 9:10am UTC](https://community.postman.com/t/collection-variables-not-updating-via-pre-script/76716/5 "2025-03-11T09:10:55Z")

</div>

Thanks for your help ,

The console output when i log response.json() :  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/7/1/71e371e29279e11610aa170f96ac0a9568c60c73.png)

When i’m in my collection in tabs “variables” i see all my collection variables with initial and current value, for all variables it updates current value but not for this variable (i use it because the collection is shared across the team)

Thanks for your time

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 11, 2025, 9:15am UTC](https://community.postman.com/t/collection-variables-not-updating-via-pre-script/76716/6 "2025-03-11T09:15:35Z")

</div>

If you’re setting any variable via a script, it’s only going to populate the current value, that would only be seen in your session.

You would need to manually persist the value of this was going to be synced and shared with other members of your team.

Is the CollectionVariable set when you make the request? Is it updating for that variables current value?

---

<div class="post-metadata">

**Author:** ![devteamadipsys](https://avatars.discourse-cdn.com/v4/letter/d/65b543/32.png) [@devteamadipsys](https://community.postman.com/u/devteamadipsys)\
**Post date:** [March 11, 2025, 9:25am UTC](https://community.postman.com/t/collection-variables-not-updating-via-pre-script/76716/7 "2025-03-11T09:25:21Z")

</div>

console.log(pm.collectionVariables) show it’s set.

I just see i’m doing 2 sendRequest, only the pm.collectionVariables.set on the last request update the variables but not the first one, if i remove the second request so the variable is updated.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 11, 2025, 9:36am UTC](https://community.postman.com/t/collection-variables-not-updating-via-pre-script/76716/8 "2025-03-11T09:36:31Z")

</div>

Can you provide visual examples to explain that flow, I can’t see what you have in front of you or what you’re doing. 🙏🏻

---

<div class="post-metadata">

**Author:** ![devteamadipsys](https://avatars.discourse-cdn.com/v4/letter/d/65b543/32.png) [@devteamadipsys](https://community.postman.com/u/devteamadipsys)\
**Post date:** [March 11, 2025, 9:45am UTC](https://community.postman.com/t/collection-variables-not-updating-via-pre-script/76716/9 "2025-03-11T09:45:25Z")

</div>

Problem solved, my bad.

Nested pm.sendRequest =\> seems to update only the variables of the last pm.sendRequest (mayby collectionVariables still have the old value in the second request)  
I’ve created 2 differents functions and it’s working.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 11, 2025, 9:48am UTC](https://community.postman.com/t/collection-variables-not-updating-via-pre-script/76716/10 "2025-03-11T09:48:17Z")

</div>

Do you have more than one async request in the script?

---

<div class="post-metadata">

**Author:** ![devteamadipsys](https://avatars.discourse-cdn.com/v4/letter/d/65b543/32.png) [@devteamadipsys](https://community.postman.com/u/devteamadipsys)\
**Post date:** [March 11, 2025, 9:50am UTC](https://community.postman.com/t/collection-variables-not-updating-via-pre-script/76716/11 "2025-03-11T09:50:09Z")

</div>

Yes, in the code sample send before, just after pm.collectionVariables.set(‘sessionId’) there is an another pm.sendRequest

It’s not working today because yesterday the sessionId was the same for the all day.

It’s all my fault, thanks for you help

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 11, 2025, 10:24am UTC](https://community.postman.com/t/collection-variables-not-updating-via-pre-script/76716/12 "2025-03-11T10:24:13Z")

</div>

Ok cool, glad you go that sorted. 🏆

I would recommend “oversharing” next time, the more information you share at the start, the quicker we can get to a solution. 🙏
