# Pass session variables between 2 scenarios

**URL:** <https://community.gatling.io/t/pass-session-variables-between-2-scenarios/4097>\
**Category:** Gatling (Open-Source)\
**Created:** [December 22, 2017, 11:09am UTC](https://community.gatling.io/t/pass-session-variables-between-2-scenarios/4097 "2017-12-22T11:09:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chi-Kien\_Diep](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@Chi-Kien\_Diep](https://community.gatling.io/u/Chi-Kien_Diep)\
**Post date:** [December 22, 2017, 11:09am UTC](https://community.gatling.io/t/pass-session-variables-between-2-scenarios/4097/1 "2017-12-22T11:09:24Z")

</div>

Hello,

I have 2 scenarios scn1 scn2, in scn1 in the response, I want to save the the ID of the message so that I can check it in the second scenario. However I was not able to pass this message\_id session into the second scenario. Can anyone help me for this case?

val scn1 = scenario(“Test1”)  
.exec(tcil(“TYBMSG\_Message”).requestReply  
.message(ByteArrayBody(\_ =\> {  
val convId = newConversationId  
val text = String.format(TYBMSG\_Message(), generationTime(), convId, newDcxId(), convId)  
print(text)  
decodeIATB(text).getBytes(UTF\_8)  
}))  
.check(regex(""“TXT+ID\:(.\*?)’”"")  
.saveAs(“ **message\_id** ”))  
)

val scn2 = scenario(“Test2”)  
.exec(tcil(“MDASUQ\_Message”).requestReply  
.message(ByteArrayBody(\_ =\> {  
val convId = newConversationId  
val text = String.format(MDASUQ\_Message(), generationTime(), convId, newDcxId(), convId)  
print(text)  
decodeIATB(text).getBytes(UTF\_8)  
}))  
.check(  
substring(" **${message\_id}**")  
))

setUp(scn1.inject(atOnceUsers(1))  
.protocols(msxPDT),  
scn2.inject(atOnceUsers(1))  
.protocols(ttfPDT))

---

<div class="post-metadata">

**Author:** ![Maksym\_Max](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@Maksym\_Max](https://community.gatling.io/u/Maksym_Max)\
**Post date:** [December 28, 2017, 10:14am UTC](https://community.gatling.io/t/pass-session-variables-between-2-scenarios/4097/2 "2017-12-28T10:14:21Z")

</div>

You can combine Chain Builders (.exec) in a Scenario Builder in that case they will be in one session. Also you can separate Chain Builders to separate object.

val scn1 =  
.exec(tcil(“TYBMSG\_Message”).requestReply  
.message(ByteArrayBody(\_ =\> {  
val convId = newConversationId  
val text = String.format(TYBMSG\_Message(), generationTime(), convId, newDcxId(), convId)  
print(text)  
decodeIATB(text).getBytes(UTF\_8)  
}))  
.check(regex(""“TXT+ID\:(.\*?)’”"")  
.saveAs(“ **message\_id** ”))  
)

val scn2 =  
.exec(tcil(“MDASUQ\_Message”).requestReply  
.message(ByteArrayBody(\_ =\> {  
val convId = newConversationId  
val text = String.format(MDASUQ\_Message(), generationTime(), convId, newDcxId(), convId)  
print(text)  
decodeIATB(text).getBytes(UTF\_8)  
}))  
.check(  
substring(" **${message\_id}**")  
))

val CompleteScen: ScenarioBuilder = scenario(“SCENARIO::SOME SCENARIO”)

```auto
  .exec(session => {

   session.set("value", "myvalue")
  })

  .exec(scn1, scn2)

```
