Handle global variables and their scope and accessing and do rewriting in tests

Hi everyone
I want to use a global variable to be used in the request like:

http://mydummyadress-{{switcher}}/ws

I created this variable in the variable quick look via the eye symbol in Postman v7.31.1 (Linux)
name: switcher -> value web-2.0.0

I want to write some tests and read out my soap statement as normal
var responseJson = xml2Json(responseBody)
… some thing more is going on

so the request-url is
console.log(request.url); --> http://mydummyadress-web-2.0.0/ws

I simple want to use to switch to another version of my api
therefore I want to simple overwrite this global variable

pm.globals.set(“switcher”, “web-1.0.0”);
postman.setNextRequest(request.name);

The aspected url should be:
console.log(request.url); --> http://mydummyadress-web-1.0.0/ws
but it is still the web-2.0.0

Therefore I did some simple variable log check with results I don’t understand:

pm.globals.set(“switcher”, “web-2.0.0”);
console.log("Init Variable: ", pm.globals.get(“switcher”)); --> Init Variable: web-2.0.0
pm.globals.set(“switcher”, “web-1.0.0”);
console.log(“changed to :”, pm.globals.get(“switcher”)) --> changed to : web-1.0.0
console.log(“do unset”) --> do unset
pm.globals.unset(“switcher”);
console.log(“Read Again”, globals.switcher); --> Read Again web-2.0.0
console.log("reread Variable: ", pm.globals.get(“switcher”)) --> reread Variable: undefined
console.log("reread one more time Variable: ", globals.switcher) --reread one more time Variable: web-2.0.

What is the difference of setting and requesting this variable?
How can I get the change in my url for resending.?

Thanks a lot.

Could you please check if you somewhere read from pm.globals.get(“switcher”) and write it into a custom made object and property called globals.switcher in your code?

The last lines in your code example tells me that you do not reread the same thing. The following code is from your example but I replaced the comments.

pm.globals.unset(“switcher”); // clears the element within the pm.globals but not other places where you saved it before via pm.globals.get(“switcher”)
console.log("reread Variable: ", pm.globals.get(“switcher”)) // undefined as the element "switcher" got cleared
console.log("reread one more time Variable: ", globals.switcher) // does not not read from the same place, this is not connected to the place where pm.globals.get(“switcher”) points/pointed to

It should be similar to the following example code.

var a = { "switcher" : "some value" };
var b = { "switcher" : a.switcher };    // link b.switcher to the same element in memory as a.switcher

console.log("a.switcher", a.switcher);    // expected output "some value"
console.log("b.switcher", b.switcher);    // expected output "some value"
delete a.switcher;    // delete/remove the link of a.switcher to the place in memory where "some value" is stored
console.log("a.switcher", a.switcher);    // expected output undefined
console.log("b.switcher", b.switcher);    // expected output "some value"

ok that is right.
there was something missing

console.log("reread Variable: ", pm.globals.get("switcher"));
console.log("reread one more time Variable: ", pm.globals.switcher)

But I still have the problem:
switcher is set to “web-2.0.1” in the variable section

http://mydummyadress-{{switcher}}/ws

console.log(pm.globals.get("switcher")); // web-2.0.1
console.log(request.url);  // http://mydummyadress-web-2.0.0/ws

// ===== change switcher
pm.globals.set("switcher", "web-1.0.0");
console.log("pm.globals.switcher);  //web-1.0.0

postman.setNextRequest(request.name);
console.log(request.url); // http://mydummyadress-web-2.0.0/ws and not web-1.0.0

The weird part
If I start the test affter the first run, the switcher variable is correctly set to web-1.0.0
So how to achieve this for sendNextRequest?