# How to use 'counterName' of repeat() at variable?

**URL:** <https://community.gatling.io/t/how-to-use-countername-of-repeat-at-variable/171>\
**Category:** Gatling (Open-Source)\
**Created:** [November 20, 2012, 6:40pm UTC](https://community.gatling.io/t/how-to-use-countername-of-repeat-at-variable/171 "2012-11-20T18:40:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Danny\_Lade](https://avatars.discourse-cdn.com/v4/letter/d/71c47a/32.png) [@Danny\_Lade](https://community.gatling.io/u/Danny_Lade)\
**Post date:** [November 20, 2012, 6:40pm UTC](https://community.gatling.io/t/how-to-use-countername-of-repeat-at-variable/171/1 "2012-11-20T18:40:11Z")

</div>

Hi,

I tried to create an bunch of objects via service using the ‘counterName’ variable of a repeat function.  
But neither the “id” is calculated correctly nor the “name” is resolved (e.g. “Test\_0”).

reasons are:

- my ID has to start at 1
- the name should be printed in the logs (e.g. “create object ‘Test\_0’”)

1. I tried to access the session object but it seems not to be existing in repeat() but exec()
2. But inside exec() I can’t define variables

And because I’m a very beginner to Scala I got no clue how to handle it.

.repeat(5, “object\_index”) {  
val id = Integer.getInteger("${object\_index}") + 1;  
val name = “Test\_${object\_index}”

exec(  
http(“create object '” + name + “’”)  
.post("/objects")  
.basicAuth(userName, userPassword)  
.body("{" +  
““id”: + id +,” +  
““name”: “” + name + “”,” +  
“}”)  
.check(status.is(200)))  
}

Any help is welcome.

Regards Danny

---

<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:** [November 20, 2012, 8:49pm UTC](https://community.gatling.io/t/how-to-use-countername-of-repeat-at-variable/171/2 "2012-11-20T20:49:32Z")

</div>

Hi Danny,

You’re actually missing the point that all the DSL elements are builders, meaning that they are only evaluated once when the Simulation is built.

In your case, the "val id = … " block is only evaluated at build time.

You have multiple solutions:

- Do everything inside the Simulation class just like you did

You have to add an extra step in order to compute your (loop index + 1)

repeat(5, “object\_index”) {  
exec(session =\> {  
val id = session.getTypedAttributeInt + 1  
session.setAttribute(“id”, id)  
})  
.exec(  
http(“create object Test\_${object\_index}”)  
.post("/objects")  
.basicAuth(userName, userPassword)  
.body("""{“id”: ${id}, “name:” Test\_${object\_index}}""")  
.check([status.is](http://status.is)(200)))  
}

- Use an external template for your JSON request body, as explained here:

[https://github.com/excilys/gatling/wiki/HTTP#wiki-template-body](https://github.com/excilys/gatling/wiki/HTTP#wiki-template-body)

I personally would go with the second solution.

Hope that helps,

Stéphane

---

<div class="post-metadata">

**Author:** ![Danny\_Lade](https://avatars.discourse-cdn.com/v4/letter/d/71c47a/32.png) [@Danny\_Lade](https://community.gatling.io/u/Danny_Lade)\
**Post date:** [November 21, 2012, 8:22am UTC](https://community.gatling.io/t/how-to-use-countername-of-repeat-at-variable/171/3 "2012-11-21T08:22:12Z")

</div>

So the description of the http() can’t be changed during runtime because it is build once - understood.  
I will try the SSP-Templates because my request bodies are getting bigger in near future.

Thanks a lot, Danny

---

<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:** [November 21, 2012, 9:41am UTC](https://community.gatling.io/t/how-to-use-countername-of-repeat-at-variable/171/4 "2012-11-21T09:41:32Z")

</div>

It can be changed at runtime, but you have to understand what is evaluated at build time and what is evaluated at runtime.  
Things evaluated at runtime are functions (usually Session =\> something) and EL (that are actually converted into functions).

In my above example, the request name “create object Test\_${object\_index}” is an EL that will be resolved every time a user sends this request.

Cheers,

Stéphane
