# Not executing the Postman.setNextRequest

**URL:** <https://community.postman.com/t/not-executing-the-postman-setnextrequest/61512>\
**Category:** Help Hub\
**Tags:** workflows\
**Created:** [May 27, 2024, 10:06am UTC](https://community.postman.com/t/not-executing-the-postman-setnextrequest/61512 "2024-05-27T10:06:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![monica.anand](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@monica.anand](https://community.postman.com/u/monica.anand)\
**Post date:** [May 27, 2024, 10:06am UTC](https://community.postman.com/t/not-executing-the-postman-setnextrequest/61512/1 "2024-05-27T10:06:51Z")

</div>

I’m trying to execute the next request in a loop and its not working here is the code please help me

```auto
pm.test("Multi Eligibility Status code is 200", function () {
 
    pm.response.to.have.status(200);
 
});
 
if (pm.response.code === 200) {
 
    const response = pm.response.text();
 
    pm.collectionVariables.set("eligibilityResponse", response);
 
    const results = JSON.parse(pm.collectionVariables.get("eligibilityResponse"));
 
    for (let i = 0; i < results.length; i++) {
        console.log("Ivalue", i);
        var requiredDocuments = results[i].requiredDocuments;
        var arr = [];
        if (requiredDocuments.length) {
            var passengerRequest = {
 
                familyName: results[i].familyName,
 
                givenName: results[i].givenName,
 
                eTicketNumber: results[i].eTicketNumber
 
            };
			console.log("doc_update_pax", passengerRequest);
            pm.collectionVariables.set("doc_update_pax", JSON.stringify(passengerRequest));
            for (var element of requiredDocuments) {
 
                if (element == "PASSPORT") {
 
                    var passport = {
 
                        "type": "PASSPORT",
 
                        "payload": {
 
                            "givenName": "ADAM",
 
                            "familyName": "BLACK",
 
                            "dateOfBirth": "1982-01-25",
 
                            "gender": "FEMALE",
 
                            "passportNumber": "5004F29700033D7D",
 
                            "expiryDate": "2032-01-25",
 
                            "nationality": "IND",
 
                            "countryOfIssue": "IND"
 
                        }
 
                    }
 
                    arr.push(passport);
                }
                if (element == "EMERGENCY_CONTACT") {
 
                    var emergency_contact = {
 
                        "type": "EMERGENCY_CONTACT",
 
                        "payload": {
 
                            "familyName": "Ronn",
 
                            "givenName": "Krude",
 
                            "phone": {
 
                                "countryCode": "+91",
 
                                "number": "7919193119"
 
                            },
 
                            "email": "gtr@xyz.com",
 
                            "refused": false
 
                        }
 
                    }
 
                    arr.push(emergency_contact);
                }
                if (element == "VISA") {
 
                    var visa = {
 
                        "type": "VISA",
 
                        "payload": {
 
                            "documentNumber": "H004825B00032F7S",
 
                            "expiryDate": "2024-07-04",
 
                            "countryOfIssue": "GBR"
 
                        }
 
                    }
 
                    arr.push(visa);
                }
                if (element == "DESTINATION_ADDRESS") {
 
                    var destination_address = {
 
                        "type": "DESTINATION_ADDRESS",
 
                        "payload": {
 
                            "street": "MAIN ST 101",
 
                            "city": "Buffalo",
 
                            "stateProv": "NY",
 
                            "postalCode": "122345",
 
                            "country": "USA"
 
                        }
 
                    }
 
                    arr.push(destination_address);
                }
                if (element == "RESIDENT_ADDRESS") {
 
                    var resident_address = {
 
                        "type": "RESIDENT_ADDRESS",
 
                        "payload": {
 
                            "countryOfResidence": "GBR"
 
                        }
 
                    }
 
                    arr.push(resident_address);
                }
 
            }
            pm.collectionVariables.set("documents", JSON.stringify(arr));
            console.log("DocumentsArray", arr);
            postman.setNextRequest("DocumentUpdate");
 
        }else{
            postman.setNextRequest(null);
        }
        
    }
}

```

---

<div class="post-metadata">

**Author:** ![gbadebo-bello](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gbadebo-bello/32/12175_2.png) [@gbadebo-bello](https://community.postman.com/u/gbadebo-bello)\
**Post date:** [May 28, 2024, 11:57am UTC](https://community.postman.com/t/not-executing-the-postman-setnextrequest/61512/2 "2024-05-28T11:57:58Z")

</div>

Hi @monica.anand. Welcome to the Postman Community!

You should use `pm.execution.setNextRequest("request_name");` instead. pm.setNextRequest is deprecated and might not give desired results.

---

<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:** [May 28, 2024, 12:41pm UTC](https://community.postman.com/t/not-executing-the-postman-setnextrequest/61512/3 "2024-05-28T12:41:45Z")

</div>

Your code is very hard to read without having any comments or details on what it is supposed to do.

Try breaking your code down into steps, and include details on what each step is meant to do.

Please note, that setNextRequest() just sets the next request that will run after the current request and all of the code in your scripts have run.

Based on your code, this will run fully through your for loop.

`for (let i = 0; i < results.length; i++) {`

This means the final setNextRequest will be the setNextRequest(null).

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [June 27, 2024, 12:42pm UTC](https://community.postman.com/t/not-executing-the-postman-setnextrequest/61512/4 "2024-06-27T12:42:13Z")

</div>

Closed on June 27, 2024, 12:42pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
