# Change Variable After a Failed Request, and Retry Using a Test Script

**URL:** <https://community.postman.com/t/change-variable-after-a-failed-request-and-retry-using-a-test-script/11328>\
**Category:** Help Hub\
**Tags:** tests, variables\
**Created:** [March 12, 2020, 3:21pm UTC](https://community.postman.com/t/change-variable-after-a-failed-request-and-retry-using-a-test-script/11328 "2020-03-12T15:21:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![j\_who](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/j_who/32/3886_2.png) [@j\_who](https://community.postman.com/u/j_who)\
**Post date:** [March 12, 2020, 3:21pm UTC](https://community.postman.com/t/change-variable-after-a-failed-request-and-retry-using-a-test-script/11328/1 "2020-03-12T15:21:42Z")

</div>

> [@Retry a failing request](https://community.postman.com/t/retry-a-failing-request/5934/1):
>
> [Retry a failing request](https://community.postman.com/t/retry-a-failing-request/5934)  
> Hope it might be of use for someone!

Thanks for this template @chrissbaumann (cc @postman-paul540) - This is just what I have been looking for! I linked this new thread outside of the “showcase” category as I’ve having some issues.

I’m looking to use it (with a small tweak) that also changes one of my request variables - specifically setting a boolean from `FALSE` to `TRUE` in the event of my most common error: 422. (A 201 is my sucessful “Service Entry Created” response).

From doing a few collection runner tests - it seems my `setNextRequest` is not initiating, but I’m not 100% - still getting the hang of this and learning as I go.  
**If anyone can spot check me here and give a bit of guidance on this test script, I’d really appreciate it!**

Thank you!

Here is my test script as currently written:

```
// Retry if the request fails (422) and correct for commonly seen mileage error issue:
var expectedHttpStatus = 201;
var maxNumberOfTries = 3;
var sleepBetweenTries = 250;

if (!pm.environment.get("tries")) {
    pm.environment.set("tries", 1);
}

if ((pm.response.code != 201) && (pm.environment.get("collection_tries") < maxNumberOfTries)) {
     var tries = parseInt(pm.environment.get("tries"), 10);
     pm.environment.set("tries", tries + 1);
     setTimeout(function() {}, sleepBetweenTries);

     // **Addition - unset and change odometer_valid variable**
     pm.variable.unset("odometer_valid");
     pm.variable.set("meter_entry_attributes[void]", "TRUE");
     
     postman.setNextRequest('Create Service Entry & Line Item(s)');
 } else {
     pm.environment.unset("tries");

     pm.test("Status code is " + 201, function () {
          pm.response.to.have.status(201);
     });
}

```

---

<div class="post-metadata">

**Author:** ![john-paul](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/john-paul/32/919_2.png) [@john-paul](https://community.postman.com/u/john-paul)\
**Post date:** [March 16, 2020, 5:35pm UTC](https://community.postman.com/t/change-variable-after-a-failed-request-and-retry-using-a-test-script/11328/2 "2020-03-16T17:35:08Z")

</div>

Hi @j_who!

One thing I notice is you’re referencing `pm.environment.get("collection_tries")`, but setting `pm.environment.set("tries", 1)` earlier. Should these be the same?

---

<div class="post-metadata">

**Author:** ![j\_who](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/j_who/32/3886_2.png) [@j\_who](https://community.postman.com/u/j_who)\
**Post date:** [March 23, 2020, 2:33pm UTC](https://community.postman.com/t/change-variable-after-a-failed-request-and-retry-using-a-test-script/11328/3 "2020-03-23T14:33:36Z")

</div>

@john-paul Thanks for your reply, I am not sure if they should be the same or not, I’m not experienced enough yet with these scripts to be able to tell you for certain…

This retry script came from this post: [Retry a failing request](https://community.postman.com/t/retry-a-failing-request/5934) and reflects the code in that template exactly.

To be more specific in the example, my collection is named “Create Service Entries” and I have environment variables saved under “Sandbox”

---

<div class="post-metadata">

**Author:** ![chrissbaumann](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/chrissbaumann/32/5521_2.png) [@chrissbaumann](https://community.postman.com/u/chrissbaumann)\
**Post date:** [March 23, 2020, 5:52pm UTC](https://community.postman.com/t/change-variable-after-a-failed-request-and-retry-using-a-test-script/11328/5 "2020-03-23T17:52:11Z")

</div>

Hey @j_who,

happy you find the snippet useful.

Indeed, @john-paul is right, there´s a mistake in my code, probably happened when editing it to post it here, cauz in my scripts I use a bit different and less readable names - my bad, sorry about it!

It should indeed be `pm.environment.set("collection_tries", 1);`  
Also, further down, it `has to be pm.environment.unset("collection_tries");`

Edit: This was kind of totally messed up. The environment variable counting tries should always be “collection\_tries”, I´ve updated the original snippet, should be good now.

---

<div class="post-metadata">

**Author:** ![j\_who](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/j_who/32/3886_2.png) [@j\_who](https://community.postman.com/u/j_who)\
**Post date:** [March 25, 2020, 1:03pm UTC](https://community.postman.com/t/change-variable-after-a-failed-request-and-retry-using-a-test-script/11328/6 "2020-03-25T13:03:50Z")

</div>

Okay, no worries! Thank you @chrissbaumann!
