# Change a collection variable dynamically with Newman

**URL:** <https://community.postman.com/t/change-a-collection-variable-dynamically-with-newman/57476>\
**Category:** Help Hub\
**Tags:** newman-project, variables, collections\
**Created:** [February 8, 2024, 6:11pm UTC](https://community.postman.com/t/change-a-collection-variable-dynamically-with-newman/57476 "2024-02-08T18:11:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![spaceflight-adminis5](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/spaceflight-adminis5/32/27398_2.png) [@spaceflight-adminis5](https://community.postman.com/u/spaceflight-adminis5)\
**Post date:** [February 8, 2024, 6:11pm UTC](https://community.postman.com/t/change-a-collection-variable-dynamically-with-newman/57476/1 "2024-02-08T18:11:56Z")

</div>

I would like to know if there is any way to change a collection variable in the cmd. I already found the --env-var “variable=value” command, but is there a similar thing for collection variables?

---

<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:** [February 9, 2024, 11:08am UTC](https://community.postman.com/t/change-a-collection-variable-dynamically-with-newman/57476/2 "2024-02-09T11:08:06Z")

</div>

Hey @spaceflight-adminis5 👋

Welcome to the Postman Community! :postman:

There isn’t a direct flag to change the Collection Variable from the command line, like with the environment or globals.

It _would_ work if you did something like this, not ideal as you’re changing things that you might not want too.

Add a placeholder variable to the Collection Variable like this:

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

And then use the `--env-var` flag in the Newman Command

```auto
newman run <collection> --env-var "fromNewman=Danny"

```

It would resolve that placeholder variable to the one added in the command:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/b/e/bed5c8f5698131194f8d6868e632757d04bbb262.png)

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

---

<div class="post-metadata">

**Author:** ![spaceflight-adminis5](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/spaceflight-adminis5/32/27398_2.png) [@spaceflight-adminis5](https://community.postman.com/u/spaceflight-adminis5)\
**Post date:** [February 9, 2024, 2:18pm UTC](https://community.postman.com/t/change-a-collection-variable-dynamically-with-newman/57476/3 "2024-02-09T14:18:29Z")

</div>

Thanks for the helping, i tried here and it works when the variable is used in the URL, but could you confirm me that this works when i use the variable in the pre/test-scripts? Because when i tried here i noticed that in the scripts didnt change, my example is like this:

```auto
const testVar = JSON.parse(pm.collectionVariables.get('testVariable'))

```

and the error is this:

```auto
Unexpected token 't' at 1:3 {{testVariable}}

```

Like if it did not change. Besides everything, thanks for the awnser, i will try to adapt the code here, but your response helped me a lot! 😁

---

<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:** [February 9, 2024, 2:32pm UTC](https://community.postman.com/t/change-a-collection-variable-dynamically-with-newman/57476/4 "2024-02-09T14:32:47Z")

</div>

I’m not sure what value you have set as `testVariable` on the command line and why that needs to be parse that way. 🤔

Also, at that point it would just be the string `{{testVariable}}` because that’s what you have set as the variable. You’d need to use `.replaceIn()` to resolve that value.

Using my example:

```auto
let non_resolved_var = pm.collectionVariables.get("testing");
let resolved_var = pm.variables.replaceIn(pm.collectionVariables.get("testing"));

console.log(non_resolved_var)
console.log(resolved_var)

```

Would display this if logged out the console:

 ![Screenshot 2024-02-09 at 14.31.50](https://us1.discourse-cdn.com/getpostman/original/3X/7/c/7ccbd7786044a1129ad0c27c82225fda70d21213.png)

---

<div class="post-metadata">

**Author:** ![spaceflight-adminis5](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/spaceflight-adminis5/32/27398_2.png) [@spaceflight-adminis5](https://community.postman.com/u/spaceflight-adminis5)\
**Post date:** [February 9, 2024, 3:04pm UTC](https://community.postman.com/t/change-a-collection-variable-dynamically-with-newman/57476/5 "2024-02-09T15:04:16Z")

</div>

Ohh thanks, i think that this can resolve my problem! Just to explain, I use these collections variables to decide if some requests will be skipped or not, so i defined with “true” in the example, that’s why i have to use the JSON.parse

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [February 12, 2024, 3:05pm UTC](https://community.postman.com/t/change-a-collection-variable-dynamically-with-newman/57476/6 "2024-02-12T15:05:02Z")

</div>

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
