# Making GET on dynamic environment variable

**URL:** <https://community.postman.com/t/making-get-on-dynamic-environment-variable/27961>\
**Category:** Help Hub\
**Created:** [September 18, 2021, 9:53pm UTC](https://community.postman.com/t/making-get-on-dynamic-environment-variable/27961 "2021-09-18T21:53:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hannahbroch](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hannahbroch](https://community.postman.com/u/hannahbroch)\
**Post date:** [September 18, 2021, 9:53pm UTC](https://community.postman.com/t/making-get-on-dynamic-environment-variable/27961/1 "2021-09-18T21:53:40Z")

</div>

I set a dynamic variable in the pre-request script using pm.environment.set(“artistName”,pm.variables.replaceIn(‘{{$randomLastName}}’));  
This set fine

I wanted to call it back in a test using pm.test(“Artist name is set correctly”, function(){  
const jsonData = pm.response.json();  
pm.expect(jsonData.contact.name).to.eql(pm.environment.get(‘{{artistName}}’));

and this gave me the response: AssertionError: expected ‘Schiller’ to deeply equal undefined

what have i done wrong?

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [September 18, 2021, 11:06pm UTC](https://community.postman.com/t/making-get-on-dynamic-environment-variable/27961/2 "2021-09-18T23:06:21Z")

</div>

you can get a varaible value in two ways:

first :

```
pm.environment.get('artistName')
pm.environment.replaceIn("This will replace {{artistName}} its actual value")
```

---

<div class="post-metadata">

**Author:** ![hannahbroch](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hannahbroch](https://community.postman.com/u/hannahbroch)\
**Post date:** [September 19, 2021, 5:17pm UTC](https://community.postman.com/t/making-get-on-dynamic-environment-variable/27961/3 "2021-09-19T17:17:29Z")

</div>

thank you, works great now

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [September 21, 2021, 3:23pm UTC](https://community.postman.com/t/making-get-on-dynamic-environment-variable/27961/4 "2021-09-21T15:23:46Z")

</div>

Just updated the answer ,for more clarity . I answered it from phone last time . ypou can use any of the above approach to get value of a variable .

and you can refer this variable in any other part except scripts as {{varname}}

---

<div class="post-metadata">

**Author:** ![hannahbroch](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hannahbroch](https://community.postman.com/u/hannahbroch)\
**Post date:** [October 3, 2021, 9:07am UTC](https://community.postman.com/t/making-get-on-dynamic-environment-variable/27961/5 "2021-10-03T09:07:02Z")

</div>

hi i wanted to ask based on this, i have another place where i had a very similar scenario  
i used the script you specified and its not working  
i have saved an environment variable using the pre-request script: -  
pm.environment.set(“minimumPayment”, pm.variables.replaceIn(’{{$randomInt}}’));  
and in the body called this back using “{{minimumPayment}}” and all seems fine  
However when i run a test i wrote it as follows:-  
pm.test(“Minimum Payment is populated by minimumPayment”, function(){

```
const jsonData = pm.response.json();

pm.expect(jsonData.minimumPayment).to.eql(pm.environment.get('minimumPayment'));

```

})

and it is failing saying something like:-  
Minimum Payment is populated by minimumPayment | AssertionError: expected 16 to deeply equal ‘16’

can you help me? thanks

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [October 3, 2021, 9:19am UTC](https://community.postman.com/t/making-get-on-dynamic-environment-variable/27961/6 "2021-10-03T09:19:37Z")

</div>

`pm.expect(jsonData.minimumPayment).to.eql(pm.environment.get('minimumPayment'));`

you have stored the payment as string than number, the deep comparison will consider the object type also . so convert it to Number type 🎉🎉🎉

```auto

pm.expect(jsonData.minimumPayment).to.eql(Number(pm.environment.get('minimumPayment')));

```

---

<div class="post-metadata">

**Author:** ![hannahbroch](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hannahbroch](https://community.postman.com/u/hannahbroch)\
**Post date:** [October 3, 2021, 9:27am UTC](https://community.postman.com/t/making-get-on-dynamic-environment-variable/27961/7 "2021-10-03T09:27:51Z")

</div>

thanks so much, wks great
