# problem with set variable on session, need explanation

**URL:** <https://community.gatling.io/t/problem-with-set-variable-on-session-need-explanation/6299>\
**Category:** Gatling (Open-Source)\
**Created:** [November 5, 2021, 4:33pm UTC](https://community.gatling.io/t/problem-with-set-variable-on-session-need-explanation/6299 "2021-11-05T16:33:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SDIRI\_Hamdi](https://avatars.discourse-cdn.com/v4/letter/s/e19b73/32.png) [@SDIRI\_Hamdi](https://community.gatling.io/u/SDIRI_Hamdi)\
**Post date:** [November 5, 2021, 4:33pm UTC](https://community.gatling.io/t/problem-with-set-variable-on-session-need-explanation/6299/1 "2021-11-05T16:33:41Z")

</div>

I am new to gatling  
I am trying to loop on json response, find the country code that I am looking for and take the id coressponding the that coutry code.  
Sample algorithm:  
list.foreach( value =\> { if (value.coutrycode == “PL”) then store value.id })

on Gatling:

def getOffer() = {  
exec(  
http(“GET /offer”)  
.get("/offer")  
.check(status.is(Constant.httpOk))

.check((bodyString.exists),  
jsonPath("$[\*]").ofType[Map[String,Any]].findAll.saveAs(“offerList”)))  
.foreach("${offerList}", “item”){

exec(session =\> {  
val itemMap = session(“item”).as[Map[String,Any]]  
val countryCodeId = itemMap(“countryCode”)  
println(“**" + countryCodeId)  
// =\> print all the country code on the list  
if (countryCodeId ==“PL”){ // if statement condition  
println("**\*\*\*\*\*\*\*\*\*”+ itemMap(“offerd”)); // print the id eg : “23”  
session.set(“offerId”, itemMap(“offerId”)); // set the id on the session  
}  
println("$$$$$$$$$$$$$$" + session(“offerId”)) // verify that th session contains the offerId but is not  
session  
})  
}  
}

When I try to print the session(“offerId”), it’s print “item” and not the offerId.  
any explanation of this behaviour please ?

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [November 5, 2021, 4:35pm UTC](https://community.gatling.io/t/problem-with-set-variable-on-session-need-explanation/6299/2 "2021-11-05T16:35:54Z")

</div>

Please read the documentation: [https://gatling.io/docs/gatling/reference/current/session/session\_api/#setting-attributes](https://gatling.io/docs/gatling/reference/current/session/session_api/#setting-attributes)

---

<div class="post-metadata">

**Author:** ![SDIRI\_Hamdi](https://avatars.discourse-cdn.com/v4/letter/s/e19b73/32.png) [@SDIRI\_Hamdi](https://community.gatling.io/u/SDIRI_Hamdi)\
**Post date:** [November 5, 2021, 5:01pm UTC](https://community.gatling.io/t/problem-with-set-variable-on-session-need-explanation/6299/3 "2021-11-05T17:01:19Z")

</div>

I tried with session(“offerId”).as[String] but I got No attribute named ‘offerId’ is defined’
