Not executing the Postman.setNextRequest

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

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": "[email protected]",
 
                            "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);
        }
        
    }
}

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.

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).

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