# Unable to create different values in map within a repeat

**URL:** <https://community.gatling.io/t/unable-to-create-different-values-in-map-within-a-repeat/4023>\
**Category:** Gatling (Open-Source)\
**Created:** [November 1, 2017, 4:13pm UTC](https://community.gatling.io/t/unable-to-create-different-values-in-map-within-a-repeat/4023 "2017-11-01T16:13:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_Hafner](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@Robert\_Hafner](https://community.gatling.io/u/Robert_Hafner)\
**Post date:** [November 1, 2017, 4:13pm UTC](https://community.gatling.io/t/unable-to-create-different-values-in-map-within-a-repeat/4023/1 "2017-11-01T16:13:18Z")

</div>

I’m new to gatling/scala and am running into an issue in which the following repeat section is not creating different values for the definitionProperties map in each iteration. I’m looking for an explanation as to why it doesn’t work as well as an alternative way to implement this logic.

```auto
.repeat(10) {
  val performanceModel : String = DefinitionUtil.getDefinitionAsString("Performance2")
  val performanceDefinitionId = "WF7FF447E1-AAA5-4ECF-850F-99702FFE3042"

  val headers = Map(
    "Authorization" -> ("${tokenType} ${accessToken}"),
    "Content-Type" -> "application/vnd.workflow.definition+json",
    "Accept" -> "application/vnd.workflow.definition.summary+json"
  )

  var definitionId = "WF".concat(java.util.UUID.randomUUID().toString)
  var name = definitionId
  var comment = "Definition ".concat(definitionId)
  var model = performanceModel.replace(performanceDefinitionId, definitionId).replace("Performance2", name)

  var definitionProperties = Map(
    "id" -> definitionId,
    "name" -> name,
    "comment" -> comment,
    "clientRegistrationId" -> clientRegistrationId,
    "model" -> model
  )

  exec(WorkflowAction.createDefinition(definitionProperties))
}

```

```auto

```

```auto
thanks,

```

```auto
Rob

```

---

<div class="post-metadata">

**Author:** ![Stephan\_D](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@Stephan\_D](https://community.gatling.io/u/Stephan_D)\
**Post date:** [November 1, 2017, 9:20pm UTC](https://community.gatling.io/t/unable-to-create-different-values-in-map-within-a-repeat/4023/2 "2017-11-01T21:20:26Z")

</div>

The variables are only evaluated once at the time the scenario is built.  
I think you have to set the values within a session.

---

<div class="post-metadata">

**Author:** ![Robert\_Hafner](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@Robert\_Hafner](https://community.gatling.io/u/Robert_Hafner)\
**Post date:** [November 2, 2017, 10:38am UTC](https://community.gatling.io/t/unable-to-create-different-values-in-map-within-a-repeat/4023/3 "2017-11-02T10:38:50Z")

</div>

Thanks for the response Stephan! I have another method (shown below) which is called with different values (for the name parameter) in my scenario and the variables are evaluated for each call. Any idea why these variables are evaluated each time based on the name parameter and the other variables are only evaluated once when the scenario is built.

```auto
def createTag(name : String) = {
  var tagProperties: Map[String, String] = Map("name" -> name)
  val body = JsonUtil.mapToJsonString(tagProperties)

  val headers = Map(
    "Authorization" -> ("${tokenType} ${accessToken}"),
    "Content-Type" -> "application/vnd.workflow.tag+json",
    "Accept" -> "application/vnd.workflow.tag+json"
  )
  exec(http("Create Tag")
    .post("/workflow/tags")
    .headers(headers)
    .body(StringBody(body)).asJSON
    .check(status.in(201)))
}

```

---

<div class="post-metadata">

**Author:** ![Stephan\_D](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@Stephan\_D](https://community.gatling.io/u/Stephan_D)\
**Post date:** [November 2, 2017, 12:15pm UTC](https://community.gatling.io/t/unable-to-create-different-values-in-map-within-a-repeat/4023/4 "2017-11-02T12:15:49Z")

</div>

With the new method you are starting a new session every time you finished your “post”, right?

But with the first example you are looping within a session. To manipulate a running session you need to use the session api gatling provides.

Maybe Stephane can verify this answer. I’m not 100% sure and pretty new to scala and gatling too 😉

---

<div class="post-metadata">

**Author:** ![Robert\_Hafner](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@Robert\_Hafner](https://community.gatling.io/u/Robert_Hafner)\
**Post date:** [November 2, 2017, 12:38pm UTC](https://community.gatling.io/t/unable-to-create-different-values-in-map-within-a-repeat/4023/5 "2017-11-02T12:38:38Z")

</div>

```auto
I'm creating my scenario using the following class. I'm not sure I understand where the session is being created. 

```

```auto

```

```auto
I believe the session is associated with each virtual user, however I'm not sure where the session begins/ends. Is the session not created until the duration block?

```

```auto

```

```auto
In the code below the call in the repeat block does not work, however, making multiple calls does. Not sure I understand why after re-reading the documentation.

```

```auto

```

```auto

```

```auto
class BasicWorkflowScenario(durationMinutes: Int, userFeeder: FeederBuilder[String]) extends
  ScenarioCreator {
...

  override def getScenario(): ScenarioBuilder = scenario(name)
      .exec(LoginAction.administratorLogin)
      .feed(userFeeder)
    .exec(LoginAction.switchToWorkflowAdmin)
    .exec(WorkflowAction.createTag("Tag1"))
    .exec(WorkflowAction.createTag("Tag2"))
    ...

```

```auto
    // This results in a http status of 409 b/c it uses the same UUID value for each iteration.
    .repeat(10) {

```

```auto
      exec(WorkflowAction.createDefinition("WF".concat(java.util.UUID.randomUUID().toString), clientRegistrationId))

```

```auto
    }

```

```auto
    // However, if I don't use repeat block, but rather make individual calls it produces unique values.

```

```auto
    exec(WorkflowAction.createDefinition("WF".concat(java.util.UUID.randomUUID().toString), clientRegistrationId))

```

```auto
    exec(WorkflowAction.createDefinition("WF".concat(java.util.UUID.randomUUID().toString), clientRegistrationId))

```

```auto
    ...

```

---

<div class="post-metadata">

**Author:** ![vu.nguyen](https://avatars.discourse-cdn.com/v4/letter/v/ba9def/32.png) [@vu.nguyen](https://community.gatling.io/u/vu.nguyen)\
**Post date:** [November 2, 2017, 8:37pm UTC](https://community.gatling.io/t/unable-to-create-different-values-in-map-within-a-repeat/4023/6 "2017-11-02T20:37:53Z")

</div>

Robert,

I think this thread provides you some guidance  
[https://groups.google.com/d/msg/gatling/Anl1Kuvbk0A/eWYlcbdNPIMJ](https://groups.google.com/d/msg/gatling/Anl1Kuvbk0A/eWYlcbdNPIMJ)

Vu

---

<div class="post-metadata">

**Author:** ![Robert\_Hafner](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@Robert\_Hafner](https://community.gatling.io/u/Robert_Hafner)\
**Post date:** [November 3, 2017, 11:36am UTC](https://community.gatling.io/t/unable-to-create-different-values-in-map-within-a-repeat/4023/7 "2017-11-03T11:36:53Z")

</div>

Thanks Vu. I updated the exec call to specify a session in an attempt to use a lambda. However, the code fails to compile with this change. Any idea what I’m doing wrong here?

07:25:27.269 [ERROR] i.g.c.ZincCompiler$ - C:\workspaces\viya\workflow-test-integration\src\test\scala\com\workflow\test\qa\perf\scenarios.scala:48: type mismatch;  
found : io.gatling.core.structure.ChainBuilder  
required: io.gatling.commons.validation.Validation[io.gatling.core.session.Session]  
07:25:27.269 [ERROR] i.g.c.ZincCompiler$ - WorkflowAction.createDefinition(  
07:25:27.270 [ERROR] i.g.c.ZincCompiler$ - ^  
07:25:27.275 [ERROR] i.g.c.ZincCompiler$ - one error found  
07:25:27.276 [ERROR] i.g.c.ZincCompiler$ - Compilation crashed  
sbt.compiler.CompileFailed: null  
at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:105)  
at sbt.compiler.AnalyzingCompiler.compile(AnalyzingCompiler.scala:47)  
at sbt.compiler.AnalyzingCompiler.compile(AnalyzingCompiler.scala:41)  
…

For reference, here are the code changes I made.

BasicWorkflowScenario.scala:

```auto
scenario(name)

```

```auto
.exec( (session:Session) =>
  WorkflowAction.createDefinition(
    "WF".concat(java.util.UUID.randomUUID().toString()),
    clientRegistrationId,
    session
  )
)

```

```auto

```

WorkflowAction.scala:

```auto
def createDefinition(definitionId: String, clientRegistrationId : String, session : Session) = {

```

```auto
 ...

```

```auto
}

```

---

<div class="post-metadata">

**Author:** ![vu.nguyen](https://avatars.discourse-cdn.com/v4/letter/v/ba9def/32.png) [@vu.nguyen](https://community.gatling.io/u/vu.nguyen)\
**Post date:** [November 4, 2017, 4:33am UTC](https://community.gatling.io/t/unable-to-create-different-values-in-map-within-a-repeat/4023/8 "2017-11-04T04:33:36Z")

</div>

I think you have two options:

Option #1  
exec { session =\>  
// function to do work  
WorkflowAction.createDefinition()

// return the original session  
session  
}

Option #2  
exec { session =\>  
// function to do work and return updated session  
WorkflowAction.createDefinition()  
}

For more information, please check following topics in documentation

- Most Gatling DSL methods takes Expression[T] parameters, which is a type alias for Session =\> Validation[T]. They are allowed to return a Session instead of Validation[Session] because of an implicit conversion.  
[https://gatling.io/docs/current/session/expression\_el/](https://gatling.io/docs/current/session/expression_el/)

- Gatling DSL exec can also be passed an Expression function.  
[https://gatling.io/docs/current/general/scenario/#exec](https://gatling.io/docs/current/general/scenario/#exec)

- Session is a virtual user’s state. Session instances are immutable!  
[https://gatling.io/docs/current/session/session\_api/](https://gatling.io/docs/current/session/session_api/)
