# Run a request only once and the rest request normal

**URL:** <https://community.postman.com/t/run-a-request-only-once-and-the-rest-request-normal/40912>\
**Category:** Help Hub\
**Tags:** tests, collections, pre-request-script\
**Created:** [November 8, 2022, 3:37pm UTC](https://community.postman.com/t/run-a-request-only-once-and-the-rest-request-normal/40912 "2022-11-08T15:37:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielf](https://avatars.discourse-cdn.com/v4/letter/d/8baadc/32.png) [@danielf](https://community.postman.com/u/danielf)\
**Post date:** [November 8, 2022, 3:37pm UTC](https://community.postman.com/t/run-a-request-only-once-and-the-rest-request-normal/40912/1 "2022-11-08T15:37:53Z")

</div>

Hello, I have a collection in newman with multiple request, that requires checking that for every instance and all the lang of that instance, different services are working. After that, I generate a report (with htmlextra, as is the one I know that allows me to modify the template of the report) with the results of the test.

To collection take a csv like the next one to be execute.

```nohighlight
instance, lang
a, en
a, es
b, en
b, es

```

The problem is that some services doesn’t require checking for lang, only for instance. For most of the request, is fine to execute the request with a-en, a-es, b-en and b-es, but, for one specifc request, is only relevant the instance, so I would only need to execute the request for a and b, only once for each instance.

Note that here I have 2 instances with 2 langs each, but the final csv I have working on have 900 entries, I cannot repeat the same request for the same instance 80 times in the prod environment.

So, I would need some way of running one request only one time for instance, not for every line in the input file.

I have already tried throwing and error in the pre-request-script when it was duplicated (I use a env var to storage the instances already used), but that stops every request, not only the request I want.

I know I could create a new collection only for that request, but that would generate a different report, and I need to have only one report. I also know that I could use allure report that allows to merge two or more reports, but I have made changes in the template, as I need to show in the report the env vars used for each request in an easy way to easy analyze without having to open each iteration.

So, is there a way using the pre-request-script or something to run a request only when the value of a env var change?

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [November 8, 2022, 5:20pm UTC](https://community.postman.com/t/run-a-request-only-once-and-the-rest-request-normal/40912/2 "2022-11-08T17:20:40Z")

</div>

Hi @danielf

Off the top of my head, I think you would need to write a conditional statement and use [postman.setnextrequest();](https://learning.postman.com/docs/running-collections/building-workflows/)

For example;

```auto
if (condition 1 is true) {
   postman.setNextRequest("HTTP Request 1");
} else if (condition 2 is true) {
  postman.setNextRequest("HTTP Request 2");
} else {
   postman.setNextRequest(null);
}

```

---

<div class="post-metadata">

**Author:** ![danielf](https://avatars.discourse-cdn.com/v4/letter/d/8baadc/32.png) [@danielf](https://community.postman.com/u/danielf)\
**Post date:** [November 9, 2022, 8:30am UTC](https://community.postman.com/t/run-a-request-only-once-and-the-rest-request-normal/40912/3 "2022-11-09T08:30:39Z")

</div>

So, being the one time request name “Request1” and the next one in the collection “Request2”, in my pre-request-script of “Request1” I would have to write something like this, right:

```javascript
if (pm.environment.get("already_run") is false) {
   pm.environment.set("already_run", true);
   postman.setNextRequest("Request1");
} else {
   postman.setNextRequest("Request2");
}

```

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [November 9, 2022, 10:03am UTC](https://community.postman.com/t/run-a-request-only-once-and-the-rest-request-normal/40912/4 "2022-11-09T10:03:17Z")

</div>

Yes.

Here are another couple of examples of it;

> <https://stackoverflow.com/questions/58977428/postman-api-setnextrequest-not-setting-next-request>

> <https://stackoverflow.com/questions/43295102/postman-how-to-loop-request-until-i-get-a-specific-response/43334857#43334857>

---

<div class="post-metadata">

**Author:** ![danielf](https://avatars.discourse-cdn.com/v4/letter/d/8baadc/32.png) [@danielf](https://community.postman.com/u/danielf)\
**Post date:** [November 9, 2022, 10:31am UTC](https://community.postman.com/t/run-a-request-only-once-and-the-rest-request-normal/40912/5 "2022-11-09T10:31:27Z")

</div>

I have read the documentation, it saids that " The `postman.setNextRequest()` function is always executed at the end of the current request.". I have tried with the next code, in “Request1”, but, as the setNextRequest is run after the request, the “Request1” was still executed in all the iterations.

```javascript
if (pm.environment.get("alreadyRun") == undefined) {
    pm.environment.set("alreadyRun", true);
} else {
    postman.setNextRequest("Request2");
}

```

So, in order to run only one time, I had to move that script to the “Request0”. As the “Request0” runs before the “Request1” (the one I want to execute only once), the script now jumps it after the first iteration to the “Request2”

Thx @w4dd325 for pointing me to setNextRequest

---

<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:** [August 24, 2023, 4:56pm UTC](https://community.postman.com/t/run-a-request-only-once-and-the-rest-request-normal/40912/6 "2023-08-24T16:56:16Z")

</div>

Closed on August 24, 2023, 4:56pm UTC

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