How to set up collection variable after the request

I want to start with Postman test. I have a collection for API which would be nice if it could run automatically. The basic problem is login. All of the endpoints need valid token. Token is stored in collection variable {{token}}. So I tried to make this on login endpoint:

var token = pm.response.json().token;
pm.collectionVariables.set("token", token);

As I run the test nothing happened in collection variable. It is still empty. Can somebody tell me plase how to set up collection variable which will be used by other endpints automatically? thans a lot.

So as I found out it works but my case is