# Prerequest script for collection to check the status code and retry the trigger

**URL:** <https://community.postman.com/t/prerequest-script-for-collection-to-check-the-status-code-and-retry-the-trigger/48093>\
**Category:** Help Hub\
**Created:** [June 2, 2023, 11:03am UTC](https://community.postman.com/t/prerequest-script-for-collection-to-check-the-status-code-and-retry-the-trigger/48093 "2023-06-02T11:03:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hptestingindia](https://avatars.discourse-cdn.com/v4/letter/h/cc9497/32.png) [@hptestingindia](https://community.postman.com/u/hptestingindia)\
**Post date:** [June 2, 2023, 11:03am UTC](https://community.postman.com/t/prerequest-script-for-collection-to-check-the-status-code-and-retry-the-trigger/48093/1 "2023-06-02T11:03:19Z")

</div>

_My question_:  
I am triggering the postman collection with Prerequest script for that collection. In that Pre request script, tried the logic to retry when any unexpected status code got. but getting error(**TypeError: Cannot read properties of undefined (reading ‘code’)** ) when running the script.

_Details (like screenshots)_:  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/3/434f86a2c649611a259f4a74644f9866904e639e.png)

Prerequest script for collection:

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

## Script:

var maxNumberOfTries = 3;  
var sleepBetweenTries = 5000;

if (!pm.environment.get(“collection\_tries”)) {  
pm.environment.set(“collection\_tries”, 1);  
}

if ((pm.response.code != 200) && (pm.environment.get(“collection\_tries”) \< maxNumberOfTries)) {  
var tries = parseInt(pm.environment.get(“collection\_tries”), 10);  
pm.environment.set(“collection\_tries”, tries + 1);  
setTimeout(function() {}, sleepBetweenTries);  
postman.setNextRequest(request.name);

}else if ((pm.response.code != 201) && (pm.environment.get(“collection\_tries”) \< maxNumberOfTries)) {  
var tries = parseInt(pm.environment.get(“collection\_tries”), 10);  
pm.environment.set(“collection\_tries”, tries + 1);  
setTimeout(function() {}, sleepBetweenTries);  
postman.setNextRequest(request.name);

}else if ((pm.response.code !=204) && (pm.environment.get(“collection\_tries”) \< maxNumberOfTries)) {  
var tries = parseInt(pm.environment.get(“collection\_tries”), 10);  
pm.environment.set(“collection\_tries”, tries + 1);  
setTimeout(function() {}, sleepBetweenTries);  
postman.setNextRequest(request.name);

}

else {

```
 pm.environment.unset("collection_tries");
 pm.test("Successful request", () => {
 pm.expect(pm.response.code).to.be.oneOf([200,201,204]);

```

});

}

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [June 2, 2023, 11:22am UTC](https://community.postman.com/t/prerequest-script-for-collection-to-check-the-status-code-and-retry-the-trigger/48093/2 "2023-06-02T11:22:10Z")

</div>

This is in a pre-request script which means it runs before the request gets sent.

Therefore “pm.response.code” will be undefined as the request hasn’t been sent yet.
