# Creating and storing a dynamic variable

**URL:** <https://community.postman.com/t/creating-and-storing-a-dynamic-variable/26096>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [July 23, 2021, 11:49am UTC](https://community.postman.com/t/creating-and-storing-a-dynamic-variable/26096 "2021-07-23T11:49:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![katiepolo](https://avatars.discourse-cdn.com/v4/letter/k/eb8c5e/32.png) [@katiepolo](https://community.postman.com/u/katiepolo)\
**Post date:** [July 23, 2021, 11:49am UTC](https://community.postman.com/t/creating-and-storing-a-dynamic-variable/26096/1 "2021-07-23T11:49:24Z")

</div>

_My question_: This might be a weird one but I’ll explain as best I can. I have a test where I need to generate a random string of 2 words and carry this through to the next two tests

I’m trying to use dynamic variables but having the following issue

In the Pre-Request script I am using something like the below

`postman.setEnvironmentVariable("generatedCategory", "#{{$randomWord}} {{$randomWord}}");`

This sets the environment variable value to `#{{$randomWord}} {{$randomWord}}` as expected.

The issue is when I use that stored variable in the body of the next test `{{generatedCategory}}` it generates **new** random words instead of the same two that were generated previously

Is there a way that after generating those first random words, I can store them to re-use?

Any help will be appreciated

_I’ve already tried_:

Just a side note - I have previously tried using the `{{$randomWord}}` dynamic variable in the test body, and pulling the value from the response to use in the next test - issue here is the response format changes, so the values move about in an array.

Eg. On one one test run I could pull and set the values from using:

```auto
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("generatedCategory", jsonData.tags[3]);

```

and in the next run it was returned in a different object in the array of tags` (jsonData.tags[2])` so is not consistent

Hope this makes sense, let me know if you need any more info

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [July 24, 2021, 1:02am UTC](https://community.postman.com/t/creating-and-storing-a-dynamic-variable/26096/2 "2021-07-24T01:02:17Z")

</div>

@katiepolo,

Yes, you already did a good job here 🙂 Still it’s not working because you need to use replaceIn() method here, please refer here for more details about dynamic variables.

> **[Dynamic variables](https://learning.postman.com/docs/writing-scripts/script-references/variables-list/)**

In your case, under Pre-request script it should as below:

```auto
pm.environment.set("generatedCategory", pm.variables.replaceIn('{{$randomWord}} {{$randomWord}}'));

```

Now you are good to use {{generatedCategory}} variable 😊

I hope this helps!!

---

<div class="post-metadata">

**Author:** ![katiepolo](https://avatars.discourse-cdn.com/v4/letter/k/eb8c5e/32.png) [@katiepolo](https://community.postman.com/u/katiepolo)\
**Post date:** [July 26, 2021, 8:06am UTC](https://community.postman.com/t/creating-and-storing-a-dynamic-variable/26096/3 "2021-07-26T08:06:13Z")

</div>

> [@bpricilla](#):
>
> ```auto
> pm.environment.set("generatedCategory", pm.variables.replaceIn('{{$randomWord}} {{$randomWord}}'));
> 
> ```

Thank you so much for this @bpricilla ! This has solved my problem 🙂 I have been using Postman for a while but love how I’m still learning new things

Thanks again 🙂

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [July 26, 2021, 9:02am UTC](https://community.postman.com/t/creating-and-storing-a-dynamic-variable/26096/4 "2021-07-26T09:02:54Z")

</div>

@katiepolo I couldn’t agree more to what you said. I feel you, same here 🙂 I am learning too 🤗

All the best 🚀 🥳
