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

Thanks for this template @chrissbaumann (cc @pfarrell) - 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.set("meter_entry_attributes[void]", "TRUE");
     postman.setNextRequest('Create Service Entry & Line Item(s)');
 } else {

     pm.test("Status code is " + 201, function () {

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?

@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 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”

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.

1 Like

Okay, no worries! Thank you @chrissbaumann!

1 Like