# Iteration Data File vs. Dynamic Variables

**URL:** <https://community.postman.com/t/iteration-data-file-vs-dynamic-variables/34609>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [April 20, 2022, 12:13pm UTC](https://community.postman.com/t/iteration-data-file-vs-dynamic-variables/34609 "2022-04-20T12:13:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![randyroman](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/randyroman/32/11753_2.png) [@randyroman](https://community.postman.com/u/randyroman)\
**Post date:** [April 20, 2022, 12:13pm UTC](https://community.postman.com/t/iteration-data-file-vs-dynamic-variables/34609/1 "2022-04-20T12:13:21Z")

</div>

Hi. Is there a way to know if a Collection run or a Single request run is using an iteration data file or not?

Basically, my use case is that if an iteration data file is being used, the test data will be sourced from that (JSON/CSV) data file. Otherwise, the test will use Dynamic Variables for the test data instead.

Currently, the way I do this is I define an environment variable “use\_data\_file” flag and set it to true or false, which I then check in the script to control the logic.

Thank you for any suggestions.

---

<div class="post-metadata">

**Author:** ![pranavdavar](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/pranavdavar/32/9152_2.png) [@pranavdavar](https://community.postman.com/u/pranavdavar)\
**Post date:** [April 21, 2022, 5:30am UTC](https://community.postman.com/t/iteration-data-file-vs-dynamic-variables/34609/2 "2022-04-21T05:30:20Z")

</div>

Hi @randyroman,

Variables in Postman follow precedence as below  
Lowest to highest.  
_global_ , _collection_ , _environment_ , _data_ , and _local_

For example, if the **environment** variable and **local** variable is having the same variable name then the value of the local variable will be picked.

For more info, you can refer to the below documentation.

> **[Using variables | Postman Learning Center](https://learning.postman.com/docs/sending-requests/variables/)**
>
> Using variables: documentation for Postman, the collaboration platform for API development. Create better APIs—faster.

Hope this helps 🙂

---

<div class="post-metadata">

**Author:** ![randyroman](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/randyroman/32/11753_2.png) [@randyroman](https://community.postman.com/u/randyroman)\
**Post date:** [April 21, 2022, 8:30am UTC](https://community.postman.com/t/iteration-data-file-vs-dynamic-variables/34609/3 "2022-04-21T08:30:33Z")

</div>

Thanks, @pranavdavar for the response. I understand the variable scoping. However, what I wanted to find out is if there is a way to know via a script if an iteration data file is being used in a particular run. Below is a snippet of the code to give more context:

```auto
if (pm.collectionVariables.get("use_datafile") === 'true') {
    console.log("Using data file!");
    pm.environment.set("firstname", pm.iterationData.get("first_name"));
    pm.environment.set("lastname", pm.iterationData.get("last_name"));
 else {
    console.log("Using dynamic variables!");
    pm.environment.set("firstname", pm.variables.replaceIn('{{$randomFirstName}}'));
    pm.environment.set("lastname", pm.variables.replaceIn('{{$randomLastName}}'));
}

```

I am using a collection variable “ **use\_datafile** ” in the **if** condition, which I have to set manually each time I want to toggle between using a data file vs. dynamic variables for my test data. I was hoping there is a way to determine via script if a data file is in use so I could avoid this manual setup. 😊

The reason why I am doing this is because a data file is not available when I am just running an individual request, and not the entire collection via postman UI or Newman. Hence I need to fallback to using dynamic variables. 😅

---

<div class="post-metadata">

**Author:** ![pranavdavar](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/pranavdavar/32/9152_2.png) [@pranavdavar](https://community.postman.com/u/pranavdavar)\
**Post date:** [April 22, 2022, 5:49am UTC](https://community.postman.com/t/iteration-data-file-vs-dynamic-variables/34609/4 "2022-04-22T05:49:44Z")

</div>

> [@randyroman](#):
>
> `pm.iterationData`

Hi @randyroman,

You may use the postman function to check if the iteration variable exists or not using the below function.

> console.log(pm.iterationData.has(‘test’));

Instead of manually switching, you may use the above function, and the output of the function is boolean.

> **[Manage API data and workflows using Postman JavaScript objects | Postman...](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/)**
>
> Manage API data and workflows using Postman JavaScript objects: documentation for Postman, the collaboration platform for API development. Create better APIs—faster.

Hope this helps 🙂

---

<div class="post-metadata">

**Author:** ![pranavdavar](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/pranavdavar/32/9152_2.png) [@pranavdavar](https://community.postman.com/u/pranavdavar)\
**Post date:** [April 22, 2022, 5:51am UTC](https://community.postman.com/t/iteration-data-file-vs-dynamic-variables/34609/5 "2022-04-22T05:51:31Z")

</div>

Your script will look like

```auto
if (pm.iterationData.has("first_name")) {
    console.log("Using data file!");
    pm.environment.set("firstname", pm.iterationData.get("first_name"));
    pm.environment.set("lastname", pm.iterationData.get("last_name"));
 else {
    console.log("Using dynamic variables!");
    pm.environment.set("firstname", pm.variables.replaceIn('{{$randomFirstName}}'));
    pm.environment.set("lastname", pm.variables.replaceIn('{{$randomLastName}}'));
}

```

---

<div class="post-metadata">

**Author:** ![randyroman](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/randyroman/32/11753_2.png) [@randyroman](https://community.postman.com/u/randyroman)\
**Post date:** [April 26, 2022, 10:05am UTC](https://community.postman.com/t/iteration-data-file-vs-dynamic-variables/34609/6 "2022-04-26T10:05:55Z")

</div>

Thank you @pranavdavar for the suggestion. This should work fine to address the problem I have. Cheers!
