# Variables not getting reset with new iteration

**URL:** <https://community.postman.com/t/variables-not-getting-reset-with-new-iteration/36041>\
**Category:** Help Hub\
**Created:** [June 8, 2022, 10:54am UTC](https://community.postman.com/t/variables-not-getting-reset-with-new-iteration/36041 "2022-06-08T10:54:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![anshulkhandelwal02](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@anshulkhandelwal02](https://community.postman.com/u/anshulkhandelwal02)\
**Post date:** [June 8, 2022, 10:54am UTC](https://community.postman.com/t/variables-not-getting-reset-with-new-iteration/36041/1 "2022-06-08T10:54:44Z")

</div>

**Your question may already have an answer on the community forum. Please search for related topics, and then read through the [guidelines](https://community.postman.com/t/about-the-just-getting-started-category/37) before creating a new topic.**

> Here’s an outline with best practices for making your inquiry.

_My question_:  
How do we update/reset the variable after each iteration?

_Details (like screenshots)_:

I am trying to use postman to do data testing on some pagination endpoints. Also, using data file to run iterations. Issue is, variable is not getting reset after iteration is finished. I am using postman’s echo endpoint as POC.

My scenario: I want to run api once. Depending on record count I get, I want to jump to last page, and validate the final page records. For this I am trying to update the pageNumber in tests and then re-running the api with updated values.

Here is the data file that I am using in runner,

> [{ “pageNumber”: 0, “pageSize”: 2 }, { “pageNumber”: 0, “pageSize”: 5 }, { “pageNumber”: 0, “pageSize”: 10 }]

Steps:

1. In postman runner, select data file. This should give 3 iterations.
2. Save response, so we can validate the executions
3. Run Postman

Iteration 1 runs fine. pageNumber value is updated correctly. Ideally, for iteration 2, pageNumber value should be reset to 0, as that’s what I provide from my data file. But postman persists the previously calculated value, and does not update this value.

URL = [https://postman-echo.com/get?pageNumber={{pageNumber}](https://postman-echo.com/get?pageNumber=%7B%7BpageNumber%7D)}&pageSize={{pageSize}}&totalRecords=46

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

Below is the execution log from console. Highlighted in red, pageNumber should get updated for new iteration, but it doesn’t.

![image](https://us1.discourse-cdn.com/getpostman/original/2X/7/7f08211e10fa5f50329160317930e3b70dacb083.png)

Could someone please suggest what am I doing wrong here? or if I am missing out something wrt how variable update works with data files.

_How I found the problem_:  
While testing the actual API. So I created a sample using postman echo api, and replicated the issue.

_I’ve already tried_:  
Moving variables to environment and collection, but no luck.

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [June 8, 2022, 2:23pm UTC](https://community.postman.com/t/variables-not-getting-reset-with-new-iteration/36041/2 "2022-06-08T14:23:00Z")

</div>

Hi @anshulkhandelwal02

What’s in your pre-req scripts?

Those console.log statements don’t appear to show the same text as what’s being output in your test tab screenshot.

Do you have other work going on inside your pre-req?

Are you trying to ‘reset’ the variables, or are you simply trying to read them sequentially from the data file?

---

<div class="post-metadata">

**Author:** ![anshulkhandelwal02](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@anshulkhandelwal02](https://community.postman.com/u/anshulkhandelwal02)\
**Post date:** [June 8, 2022, 5:29pm UTC](https://community.postman.com/t/variables-not-getting-reset-with-new-iteration/36041/3 "2022-06-08T17:29:46Z")

</div>

This is my pre-request script.

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

What my ask is that, with every iteration from data file, variables should be set as per value form data file. Currently, the value of pageNumber is not getting updated when new iteration starts. It’s picking up the last value.

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [June 8, 2022, 7:11pm UTC](https://community.postman.com/t/variables-not-getting-reset-with-new-iteration/36041/4 "2022-06-08T19:11:38Z")

</div>

> [{ “pageNumber”: 0, “pageSize”: 2 }, { “pageNumber”: 0, “pageSize”: 5 }, { “pageNumber”: 0, “pageSize”: 10 }]

Is this the entire data file, or are there other lines?  
I tried to add it to a test script as-is and it didn’t like it (I assume because it’s incorrectly formatted JSON?).

It’s an odd one this because on face value I would expect what you have to work… 🤔

---

<div class="post-metadata">

**Author:** ![anshulkhandelwal02](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@anshulkhandelwal02](https://community.postman.com/u/anshulkhandelwal02)\
**Post date:** [June 9, 2022, 7:00am UTC](https://community.postman.com/t/variables-not-getting-reset-with-new-iteration/36041/5 "2022-06-09T07:00:57Z")

</div>

Here is the updated one,

`[{"pageNumber": 0,"pageSize": 2},{"pageNumber": 0,"pageSize": 5},{"pageNumber": 0,"pageSize": 10}]`

And yes, this is entire data file.  
My actual scenario is hitting my project API, and verify pagination scenarios.  
I created this sample to show how the variables are not getting updated when new iteration starts.

Iteration 1 will have 2 requests, with pageNumber getting set as 23 in 2nd request.  
In iteration 2, pageNumber should get set as 0 at start as that’s what the value is in data file. But value still remains 23.

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [June 9, 2022, 9:32am UTC](https://community.postman.com/t/variables-not-getting-reset-with-new-iteration/36041/6 "2022-06-09T09:32:01Z")

</div>

This line, “sets” the variable;

```auto
pm.variables.set("pageNumber", (Math.floor(responseData.args.totalRecords / responseData.args.pageSize)));

```

Is taking the total records, which is hardcoded in your URL as ‘46’;  
 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/8/82fd083b78a17c9e77412a4ccd93cbd6d69c0ba1.png)

And then divides it by {{pageSize}}  
 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/d/d5e462ff7719b3ebb363bd25422d082addcf2981.png)

46 / 2 = 23

Hope this helps!

---

<div class="post-metadata">

**Author:** ![anshulkhandelwal02](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@anshulkhandelwal02](https://community.postman.com/u/anshulkhandelwal02)\
**Post date:** [June 9, 2022, 10:46am UTC](https://community.postman.com/t/variables-not-getting-reset-with-new-iteration/36041/7 "2022-06-09T10:46:58Z")

</div>

You are missing the point. I am talking about pageNumber. If you see my ques, I have added an image of console logs. Iteration 0 is all good. In Iteration 1, PageNumber should get set as 0 as that’s what the value is in the data file. But it’s still remaining as 23. Then what’s the point of using data files, as the variable is not taking its value from the data file.

Expected Result:  
Iteration 0 - Request 1 - page number = 0  
Iteration 0 - Request 2 - page number = 23  
Iteration 1 - Request 1 - page number = 0  
Iteration 1 - Request 2 - page number = 9  
Iteration 2 - Request 1 - page number = 0  
Iteration 2 - Request 2 - page number = 4

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [June 9, 2022, 11:38am UTC](https://community.postman.com/t/variables-not-getting-reset-with-new-iteration/36041/8 "2022-06-09T11:38:07Z")

</div>

Yes, you are right, I didn’t fully read the question, apologies…  
I also didn’t realise there was a second request being made…

So, try this…

Use “pm.iterationData” to grab the variable from the data file.  
Put this line of code in your pre-req in request 1.

```auto
pm.variables.set("pageNumber", pm.iterationData.get("pageNumber"));

```

Like this;

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

* * *

Then change “setNextRequest” to trigger the 2nd request

```auto
postman.setNextRequest('Request 2');

```

Like this;

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/6/635126ec9a9a1330b70fb990d4e92109a16cbd22.png)

* * *

Then change the “setNextRequest” in request 2, to point back at request 1  
(Request 1 triggers Request 2 and Request 2 triggers Request 1)

```auto
postman.setNextRequest('Request 1');

```

Like this;

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/0/05fc2d5569a0fa1295ec6c91086864df26381e2f.png)

This is my output;

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/0/0717594745d22b0a2e27c516d1e80dbeddacde7c.png)

I think this is what you were asking for?
