I have one get call which gives me record id and status, which I am storing in a collection variable.
I am using that id and status one by one using shift function.
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”
In both cases the put request is not calling even though I run a whole collection.
Also, after Get call where I have written my script in “Test” to run the conditional statement it stops with 500 server error.
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.
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.
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.
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.
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.
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
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.