Ignoring a request from the runner based on iteration

I have to run my collection on two different Country levels so I have my iteration set up as one iteration for one country, but some of the end points are not country specific and need not have to run twice.

Is there a way I can avoid running certain end points only once in the runner based on iteration.

You can use postman.setNextRequest(<request name>) to skip over requests in the collection runner. As long as you know what data triggers a different workflow you can skip over requests that way.

I’m happy to provide you an example if you provide some more details on what your flow is and what data would cause you to skip over some requests.

So I have about 40 requests in my runner but out of which about 15 are generic ones and not country specific. I cannot group them together as they are spread across in specific order and need to be in that way.

The iteration is based on country level so for example one iteration is for UK and the second one is for US.

So using postman.setNextRequest() as it is doesnt help , I will need to make it specific for iteration count if possible. Like run this request if the iteration count is 1 or something like that.

Hi @Usivaram7,

Welcome to the community! :clap:

Given the criteria you mentioned, and @allenheltondev’s recommendation, what you can do is create an iteration counter in your pre-request script (or test script if thats better), and store that count as a Postman Variable (global or environment).

Then, you can check for the iteration count variable in your test script, and depending on that count, you can execute the pm.setNextRequest(<request name or request ID>) to execute the request that should follow.

Best,
Orest

Yes I think that would work ,Are you able to suggest a example code for it ?

I tried this way as well by adding a Iteration Count column in my data.csv

if (pm.iterationData.get(“Iteration Count”) === 1) {

postman.setNextRequest(‘request name1’);

} else {

postman.setNextRequest(‘request name2’);

}

1 Like

@Usivaram7 nice, this should also suffice as a means to get an iteration count.

Let us know if that works!

Orest

no sadly it didnt work. Hence asking if there is anything else I could try.

Can you try using the request ID instead of the request name? I have had better luck there. You can get the request ID by exporting the collection, and searching for the request ID in the collection json file.

Also, try a “==” instead of a “===”. If my memory serves me correctly, === implies object equality, but not value equality.

1 Like

if(“value” == pm.iterationData.get(“fieldname”))

{

postman.setNextRequest("Next request");

}

Try this in pre req tests.

2 Likes

All looking good now. Thank you so very much.

You are welcome :heart_eyes:

Hi I want to use the below csv


for each row being passed to the relevant request. But not sure where do I give the pre-script request, in the folder or the request

if(‘{{Test_ID}}’ === 1){
pm.sendRequest(‘TC_01’);
}
else {
pm.sendRequest(‘TC_04a’);
}

@aerospace-architec10

Can you please raise your own issue rather than tagging onto a closed issue from 4 years ago.

Provide screenshots of your request and more details on your flow.

I can however tell you, that you can’t target the variables from the CSV using that format. You can’t use the method {{variableName}} in scripts.

Using variables in scripts | Postman Learning Center

In particular…

Using data variables | Postman Learning Center

You can also get access to the current iterations data by using the special “data” variable.

The data variable will have a JSON representation of the current line in your CSV.

So you can target the Test_ID column using…

if (data.Test_ID === 1) {
   // do something
}

Are you sure you want to use sendRequest and not setNextRequest()??

setNextRequest just sets the next request that will run after all of the code in pre-request scripts, the current request, and the code in the various tests tab have run. It won’t just run test case 1 or 4. The current request will still run. sendRequest is something completely different.

To avoid any confusion, I would therefore recommend providing more details on the flow you are trying to achieve. (By raising your own issue with all of the details in). If you paste code, please use the preformatted text option in the editor to stop all of the code aligning to the left.