# Check the foreach counter value does not exceed a given value in Gatling2

**URL:** https://community.gatling.io/t/check-the-foreach-counter-value-does-not-exceed-a-given-value-in-gatling2/391
**Category:** Gatling (Open-Source)
**Created:** [May 22, 2013, 11:53am UTC](https://community.gatling.io/t/check-the-foreach-counter-value-does-not-exceed-a-given-value-in-gatling2/391 "2013-05-22T11:53:03Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![natalja.librante](https://avatars.discourse-cdn.com/v4/letter/n/eb8c5e/32.png) [@natalja.librante](https://community.gatling.io/u/natalja.librante)
#### Post date: [May 22, 2013, 11:53am UTC](https://community.gatling.io/t/check-the-foreach-counter-value-does-not-exceed-a-given-value-in-gatling2/391/1 "2013-05-22T11:53:03Z")

</div>

Hi!

In my test I request details of user loans. Each user could have a number of loans, but I need to request not more than five of them.

My scenario is:  
val request\_loans\_list = execWithStdChecks(http(“Loans list”)  
.post("/accounts/accounts-list.xml")  
.headers(headers\_Content\_Length\_0)  
.check(  
xpath("//account[starts-with(producttypeid/text(), ‘51’)]/accountid")  
.findAll  
.whatever  
.saveAs(“loan\_ids”)  
)  
)

val request\_loans\_info = execWithStdChecks(http(“Loans info”)  
.get("/loans/loans\_xml.xml")  
.queryParam(""“id”"", “${loan\_id}”)  
)

val request\_all\_loan\_info = doIf(session =\> session.contains(“loan\_ids”)){  
foreach("${loan\_ids}", “loan\_id”, “counter”){  
doIf(session =\> session.getAttribute(“counter”) \< 5){  
exec(request\_loans\_info)  
}  
}  
}

As I understood I have to use doIf inside foreach loop, but it is not compiled. I use Gatling2 for my tests.

---

<div class="post-metadata">

### Author: ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)
#### Post date: [May 22, 2013, 12:08pm UTC](https://community.gatling.io/t/check-the-foreach-counter-value-does-not-exceed-a-given-value-in-gatling2/391/2 "2013-05-22T12:08:44Z")

</div>

Yep, this is the Gatling 1 syntax.

In Gatling 2.0.0-M2, you have to write session =\> session.getVInt.map(\_ \< 5)

Beware, that will change in the next milestone (we’ve been discussing the new syntax [here](https://github.com/excilys/gatling/issues/1175)). Then, you’ll be able to write:

- session =\> session(“counter”).as[Int] \< 5
- or, session =\> session(“counter”).validate[int].map(\_ \< 5)  
Cheers,

Stéphane

---

<div class="post-metadata">

### Author: ![natalja.librante](https://avatars.discourse-cdn.com/v4/letter/n/eb8c5e/32.png) [@natalja.librante](https://community.gatling.io/u/natalja.librante)
#### Post date: [May 22, 2013, 12:33pm UTC](https://community.gatling.io/t/check-the-foreach-counter-value-does-not-exceed-a-given-value-in-gatling2/391/3 "2013-05-22T12:33:26Z")

</div>

Thank you for so quick answer!  
It works perfectly

---

<div class="post-metadata">

### Author: ![natalja.librante](https://avatars.discourse-cdn.com/v4/letter/n/eb8c5e/32.png) [@natalja.librante](https://community.gatling.io/u/natalja.librante)
#### Post date: [December 15, 2014, 1:58pm UTC](https://community.gatling.io/t/check-the-foreach-counter-value-does-not-exceed-a-given-value-in-gatling2/391/4 "2014-12-15T13:58:58Z")

</div>

Hello!

I updating my old test scenario for Gatling M2.0.3

In my test I request details of user loans. Each user could have a number of loans, but I need to request not more than five of them.  
In Gatling 2.0.0-M2 I have perfectly working check:  
foreach("${account\_ids}", “account\_id”, “counter”){  
doIf(session =\> session.getVInt.map(\_ \< 5)){

But now i have problem with syntactics  
I try earlier suggested  
foreach("${account\_ids}", “account\_id”, “counter”){  
doIf (

- session =\> session(“counter”).as[Int] \< 5
- or, session =\> session(“counter”).validate[int].map(\_ \< 5) {

But it didn’t compile anymore.

---

<div class="post-metadata">

### Author: ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)
#### Post date: [December 15, 2014, 2:18pm UTC](https://community.gatling.io/t/check-the-foreach-counter-value-does-not-exceed-a-given-value-in-gatling2/391/5 "2014-12-15T14:18:16Z")

</div>

>

---

<div class="post-metadata">

### Author: ![natalja.librante](https://avatars.discourse-cdn.com/v4/letter/n/eb8c5e/32.png) [@natalja.librante](https://community.gatling.io/u/natalja.librante)
#### Post date: [December 17, 2014, 1:39pm UTC](https://community.gatling.io/t/check-the-foreach-counter-value-does-not-exceed-a-given-value-in-gatling2/391/6 "2014-12-17T13:39:34Z")

</div>

I don’t know what it was for issue, but now code compile with: session =\> session(“counter”).as[Int] \< 5

---

<div class="post-metadata">

### Author: ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)
#### Post date: [December 17, 2014, 2:48pm UTC](https://community.gatling.io/t/check-the-foreach-counter-value-does-not-exceed-a-given-value-in-gatling2/391/7 "2014-12-17T14:48:58Z")

</div>

As it should 🙂
