setNextRequest is not working in Collection

  1. I have one get call which gives me record id and status, which I am storing in a collection variable.
  2. I am using that id and status one by one using shift function.
  3. Based on the record status I have added the condtional statement that if status = “X”, call put request by using “setNextRequest()” and else if status = “Y” call another put call using “setNextRequest”
  4. In both cases the put request is not calling even though I run a whole collection.
  5. Also, after Get call where I have written my script in “Test” to run the conditional statement it stops with 500 server error.
  6. I had tried console logging id and status and my if statement is working fine. It’s just not calling the next put call based on condition.

Please advise.

1 Like

Thanks for breaking down your issue into the smaller steps. It helps a lot with understanding what you are trying to achieve.

Can you please post the code you have where its calling setNextRequest().

Please use the preformatted text option “</>” in the editor so the code keeps its formatting.

In relation to point 5. If you get a 500 server error, it sounds like your initial GET request is not completing properly which potentially will prevent setNextRequest from running the next request (as the current request has not completed).

Can you get your GET request to complete if you don’t have any code in the pre-request or tests tab? If not, you need to investigate that first.

How are your requests structured. Are they in single collection, are they in a single folder, or are the PUT requests separated somehow. It might be useful to post a screenshot of your collection structure.

One final point is that setNextRequest() only works through the collection runner. Just in case that is the issue.

Hello Mike,

Thank you for your response.

Here is my collection -

image

and here is my code of Get call. I am not sure how to copy paste the code in its original format here.

Also, to clarify. i have tried calling the whole collection with get and put call as well as only Get call by assuming based on the condition the put will get call but still not getting any response. Now i am getting the 200 msg in my response but after that it stops and does not call any of the put call.

const response = pm.response.json();

const Ids = response.map(record => record.id);

const Status = response.map(record => record.status);

pm.collectionVariables.set('currentId', Ids.shift());

pm.collectionVariables.set('currentStatus', Status.shift());

if(Array.isArray(Ids) && Ids.length > 0) {

if(Status == "X"){

postman.setNextRequest('First_putcall');

}

else if(Status == "Y")

{

postman.setNextRequest('Second_putcall' );

} else {}

} else {

postman.setNextRequest(null);

}

In put call, my code is as below…

in pre-script

const ds = pm.variables.get('Ids');
pm.variables.set('currentId', Ids.shift());

In Test Script -

console.log("Updated Record:" + pm.collectionVariables.get('currentId'));
pm.test("Successful PUT/POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201, 202]);
    postman.setNextRequest('Get Record Details');
});

Please advise.
I appreciate your help!

Thanks,
Deepika

I recommend using the console log to show the array’s to ensure the arrays are being set correctly.

Inside your IF statement blocks. Also use the console log to see if you are hitting those elements. (Basically Console log all variables).

This will help in troubleshooting.

In your GET request, you are getting the Ids and Status and then using JavaScript shift() functions to get the current value, but I can’t see where you are storing the array?

Your PUT request has the following which will probably be null. The local variable in the GET request will not be available for the PUT request. You need to use another collection variable to store the Ids and status arrays.

const ds = pm.variables.get('Ids');
pm.variables.set('currentId', Ids.shift());

However, don’t take my word for this. Use the console log to log the elements.

Pretty sure you also need to stringify the array when storing it as a collection variable, and JSON.parse it when retrieving.

Lastly, are you are running this through the collection runner? As setNextRequest only works in the runner.

Just a word of advice here - when a test fails/reaches an invalid assertion, it aborts the test code immediately. So if your assertion of pm.expect(pm.response.code).to.be.oneOf([201, 202]); fails, it won’t call setNextRequest.

This might be intentional but just wanted to point it out. You may want to make sure to have a failure route set if you need it.

Thank you Mike,

I added stringify and also did lot many changes in the script.
Now the script looks like as below.

GET Call :

const response = pm.response.json();
const Ids = response.map(record => record.id);
const Status = response.map(record => record.status);

pm.collectionVariables.set('Ids',  JSON.stringify(Ids));
pm.collectionVariables.set('Status',  JSON.stringify(Status));

//console.log((Ids));
//console.log(Status);

let currentId = Ids.shift();
let currentStatus = Status.shift();

pm.collectionVariables.set('currentId',  JSON.stringify('currentId'));
pm.collectionVariables.set('currentStatus',  JSON.stringify('currentStatus'));

//console.log(('currentId'));
//console.log('currentStatus');

if ('currentId'!== null || 'currentId'!== undefined) {
    //console.log('currentId');
    //console.log('currentStatus');
if('currentStatus'== "X"){     
 console.log('currentId');   
        postman.setNextRequest('9033b2cq-1152-413f-83fd-9206e0vfr9qw');}   
else if(currentStatus == "Y"){
 console.log('currentId');
        postman.setNextRequest('567890e9-a7ff-22vg-bab2-afe61c648b53');
        }else{}
    }

“X” PUT Call

Pre-Script
var getid = pm.collectionVariables.get("currentId");

Body

  "id": {{currentId}},
  "update-reason": {
    "code": "DUPLICATE-record"
  },
  "comment": "This is a duplicate record and processed through another system"
}```

Test Script
```console.log("X Call Id :" + pm.collectionVariables.get('currentId'));
pm.test("Successful PUT/POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201, 202]);
    pm.collectionVariables.unset("currentId");
    pm.collectionVariables.unset("currentStatus");
    //postman.setNextRequest('5d564d50-777f-4640-9b40-989f1b0610be'); // Calling Get Call again
});```


"Y" PUT Call

Pre-Script 
```var getid = pm.collectionVariables.get("currentId");
//console.log(getid);
if(getid !== null){
     postman.setNextRequest('Y Call'); Calling the same request to update the record
}```

Body
```{
  "id": {{currentId}}
}```

Test Script 

``` console.log("Y Call Id :" + pm.collectionVariables.get('currentId'));
pm.test("Successful PUT/POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201, 202]);
    pm.collectionVariables.unset("currentId");
    pm.collectionVariables.unset("currentStatus");
    postman.setNextRequest('5d564d50-777f-4640-9b40-989f1b0610be'); // Calling Get Call again
});```


This code is working fine and updating all the records except its running one more time at the end and giving 400 error. please see the message below. 
Its passing the id and status as null and running the loop, not sure why as i have added the condition as well to check if id !== null or undefined . 

URL: https://url/api/document/null/update
Method: PUT
Status Code: 404 Not Found

URL: https://url/api/document/null/delete
Method: PUT
Status Code: 404 Not Found

Any advice would be appreciated. 

Thanks,
Deepika

Has this fixed the issue, is the setNextRequest loop now working as expected?

If its not working, can you please copy and paste an example response for the GET request plus the latest version of your code.

Hello MIke,

Yes, i have added the comment above.

You have to use setNextRequest(‘null’) to end the loop, otherwise Postman will run the next request in the collection which is why you are getting one more execution than expected (with undefined data).

The way I would do this rather than checking for undefined or null is to check the array length in the two put requests.

You retrieve the array from the collection variable and if the length is more than zero, then you continue to loop.

For example…

if (ids.length > 0){
    postman.setNextRequest("getRequest");

} else {
    postman.setNextRequest(null);
}

okay thanks, it is working and no longer giving the 400 error but this time its skipping the last record because when i use shift function it assign this to current id but delete that entry from id (the array) and when the if statement check for the id length, it does not execute it because it is 0 but the currentid still has one record left to process.

Hello Mike,

I finally got this working, instead of “Id”, i used (currentid != null ) in the If statement and it worked without any error.

 if ('currentId' != null) {
        if('currentStatus'== "X"){ 
            postman.setNextRequest('9033b2cq-1152-413f-83fd-9206e0vfr9qw');}   
        else if(currentStatus == "Y"){
         postman.setNextRequest('567890e9-a7ff-22vg-bab2-afe61c648b53');}
        else{}
    }
    else {postman.setNextRequest(null);}
1 Like

Beware of using ID’s as they change if you need to copy or fork a collection.

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