# Send a parameter to function that contains a session attribute

**URL:** <https://community.gatling.io/t/send-a-parameter-to-function-that-contains-a-session-attribute/5162>\
**Category:** Gatling (Open-Source)\
**Created:** [July 29, 2019, 7:09pm UTC](https://community.gatling.io/t/send-a-parameter-to-function-that-contains-a-session-attribute/5162 "2019-07-29T19:09:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fdavidf](https://avatars.discourse-cdn.com/v4/letter/f/4bbf92/32.png) [@fdavidf](https://community.gatling.io/u/fdavidf)\
**Post date:** [July 29, 2019, 7:09pm UTC](https://community.gatling.io/t/send-a-parameter-to-function-that-contains-a-session-attribute/5162/1 "2019-07-29T19:09:24Z")

</div>

Hi,

I'm trying to send a parameter to function (runCommand) that contains a session attribute.

Once I call it by this way, the method theiaTasksUtil.runCommand is not called at all.

\* var deployToCF = ""\*

\* val fioriS4Scenario = scenario("Fiori S4 Scenario") .feed(mainFeeder) //contains username .exec{session =\> deployToCF = "cf deploy /home/user/projects/mta\_bp/mta\_archives/mta\_bp\_" + session("username").as[String] + "\_0.0.1.mtar" println("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*deployToCf: " + deployToCF) //prints \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*deployToCf: cf deploy /home/user/projects/mta\_bp/mta\_archives/mta\_bp\_user1\_0.0.1.mtar exec(theiaTasksUtil.runCommand(deployToCF, "deploy MTAR to CF")) session }\*

In case I do it in this way (using global variable), deployToCF is sent as empty string (initial value)

\* var deployToCF = ""\*

\* val fioriS4Scenario = scenario("Fiori S4 Scenario") .feed(mainFeeder) //contains username .exec{session =\> deployToCF = "cf deploy /home/user/projects/mta\_bp/mta\_archives/mta\_bp\_" + session("username").as[String] + "\_0.0.1.mtar" println("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*deployToCf: " + deployToCF) //prints \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*deployToCf: cf deploy /home/user/projects/mta\_bp/mta\_archives/mta\_bp\_user1\_0.0.1.mtar session }\*

&nbsp;&nbsp;&nbsp;&nbsp;\* .exec(theiaTasksUtil.runCommand(deployToCF, "deploy MTAR to CF"))\*

Could you please tell me how to pass the session attribute as a parameter?

Thanks,

David

---

<div class="post-metadata">

**Author:** ![phiSgr](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/phisgr/32/1437_2.png) [@phiSgr](https://community.gatling.io/u/phiSgr)\
**Post date:** [July 30, 2019, 3:24am UTC](https://community.gatling.io/t/send-a-parameter-to-function-that-contains-a-session-attribute/5162/2 "2019-07-30T03:24:01Z")

</div>

Hi David,

Look at the warning in the [documentation](https://gatling.io/docs/current/general/scenario#exec).

> Gatling DSL components are immutable `ActionBuilder`(s) that have to be chained altogether and are only built once on startup. The results is a workflow chain of `Action`(s). These builders don’t do anything by themselves, they don’t trigger any side effect, they are just definitions. As a result, creating such DSL components at runtime in functions is completely meaningless.

If you want to do string interpolation with the session attributes, there is the [expression language](https://gatling.io/docs/current/session/expression_el/) for that.

Regards,  
George

---

<div class="post-metadata">

**Author:** ![fdavidf](https://avatars.discourse-cdn.com/v4/letter/f/4bbf92/32.png) [@fdavidf](https://community.gatling.io/u/fdavidf)\
**Post date:** [July 30, 2019, 8:37am UTC](https://community.gatling.io/t/send-a-parameter-to-function-that-contains-a-session-attribute/5162/3 "2019-07-30T08:37:36Z")

</div>

Hi George,

Thank you for your reply.

I’m still struggling to send a session parameter as a parameter to the custom method.

The script below calls runCommand with a string “${deployToCF}” and not the value of ${deployToCF}.

Any idea how to send the value?

```auto
exec { session =>
  session.set("deployToCF", "cf deploy /home/user/projects/mta_bp/mta_archives/mta_bp_" + session("username").as[String] + "_0.0.1.mtar")
}
.exec { session =>
  println("**********************deployToCF:" + session("deployToCF").as[String] ) --> prints the correct value:**********************deployToCF:cf deploy /home/user/projects/mta_bp/mta_archives/mta_bp_ **user1** _0.0.1.mtar
  session
}
.exec(theiaTasksUtil.runCommand("${deployToCF}", "deploy MTAR to CF")) --> sends a string "${deployToCF}" and not the value of ${deployToCF}

```

---

<div class="post-metadata">

**Author:** ![Manjunath\_Kontham](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Manjunath\_Kontham](https://community.gatling.io/u/Manjunath_Kontham)\
**Post date:** [July 30, 2019, 10:07am UTC](https://community.gatling.io/t/send-a-parameter-to-function-that-contains-a-session-attribute/5162/4 "2019-07-30T10:07:14Z")

</div>

Since the variable is in the session, first you need to get it to a local variable (Scala variable) and then pass the scala variable to your custom function.

```auto
.exec { session =>
  println(" ********************** deployToCF:" + session("deployToCF").as[String] ) --> prints the correct value:

```

```auto
val deployToCF = session("deployToCF").as[String]

```

---

<div class="post-metadata">

**Author:** ![phiSgr](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/phisgr/32/1437_2.png) [@phiSgr](https://community.gatling.io/u/phiSgr)\
**Post date:** [July 30, 2019, 10:07am UTC](https://community.gatling.io/t/send-a-parameter-to-function-that-contains-a-session-attribute/5162/5 "2019-07-30T10:07:21Z")

</div>

Since it is a custom method, we don’t know it’s implementation, or even the type signature unless you post it. So here is my speculation.

It probably takes a String rather than an Expression[String], and the runCommand function does not put the value from the session into the request.

---

<div class="post-metadata">

**Author:** ![fdavidf](https://avatars.discourse-cdn.com/v4/letter/f/4bbf92/32.png) [@fdavidf](https://community.gatling.io/u/fdavidf)\
**Post date:** [July 30, 2019, 12:45pm UTC](https://community.gatling.io/t/send-a-parameter-to-function-that-contains-a-session-attribute/5162/6 "2019-07-30T12:45:33Z")

</div>

Your guess is correct. The signature of runCommand is String, String.  
By sending it with runCommand ("${deployToCF}", “deploy MTAR to CF”)) I expect Gatling compiler to convert it to the value, but it does not happen ☹

```auto

```

```auto
def runCommand(command: String, commandDescription: String): ChainBuilder = {
  setTaskParams(command, commandDescription)
    .exec(sendTask(command, commandDescription))
}

```

---

<div class="post-metadata">

**Author:** ![fdavidf](https://avatars.discourse-cdn.com/v4/letter/f/4bbf92/32.png) [@fdavidf](https://community.gatling.io/u/fdavidf)\
**Post date:** [July 30, 2019, 12:47pm UTC](https://community.gatling.io/t/send-a-parameter-to-function-that-contains-a-session-attribute/5162/7 "2019-07-30T12:47:04Z")

</div>

Unfortunately, in this case, the runCommand() method is not triggered at all (from the session block)
