Collection variables not updating via pre-script

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

Hey @devteamadipsys :waving_hand:t3:

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?

The script :

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.

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.:folded_hands:t2:

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?

Thanks for your help ,

The console output when i log response.json() :
image

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

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?

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.

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. :folded_hands:t2:

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.

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

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

1 Like

Ok cool, glad you go that sorted. :trophy:

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

1 Like