# How to define a global variable on the fly and access tit through Different suites in the Collection?

**URL:** <https://forum.katalon.com/t/how-to-define-a-global-variable-on-the-fly-and-access-tit-through-different-suites-in-the-collection/32575>\
**Category:** API Testing\
**Tags:** api-testing, katalon-studio\
**Created:** [September 4, 2019, 1:39pm UTC](https://forum.katalon.com/t/how-to-define-a-global-variable-on-the-fly-and-access-tit-through-different-suites-in-the-collection/32575 "2019-09-04T13:39:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nirav.makwana](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/nirav.makwana/32/8032_2.png) [@nirav.makwana](https://forum.katalon.com/u/nirav.makwana)\
**Post date:** [September 4, 2019, 1:39pm UTC](https://forum.katalon.com/t/how-to-define-a-global-variable-on-the-fly-and-access-tit-through-different-suites-in-the-collection/32575/1 "2019-09-04T13:39:48Z")

</div>

I am creating a Global variable while execution is in progress in 1 Suite, and I am trying it to access it in next Suite that is getting executed through Testsuite-collection. But this is not happening.

The code I am trying to create Global variable is as below :  
@Keyword  
void addGlobalVariable(String name, def value) {  
GroovyShell shell1 = new GroovyShell()  
MetaClass mc = shell1.evaluate(“internal.GlobalVariable”).metaClass  
String getterName = “get” + name.capitalize()  
mc.‘static’."$getterName" = { → return value }  
mc.‘static’."$name" = value  
}

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [September 5, 2019, 1:43am UTC](https://forum.katalon.com/t/how-to-define-a-global-variable-on-the-fly-and-access-tit-through-different-suites-in-the-collection/32575/2 "2019-09-05T01:43:20Z")

</div>

The instance of `internal.GlobalVariable` is scoped per each execution of single TestSuite.This instance is newly created and destroyed per each TestSuite run.

Therefore, even if your TestSuite1 created a property in the instance of `internal.GlobalVariable` class, it will be destroyed before TestSuite2 runs. The instance of `internal.GlobalVariable` in TestSuite2 will be newly created and will not have the property that you want to see.

* * *

If I were you, I would create a JSON file somewhere in the project as a temporary storage for values to pass around from TestSuite1 to TestSuite2. TestSuite1 writes values into the file, TestSuite2 reads values from the file. That’s all. No Groovy Metaprogramming required.

---

<div class="post-metadata">

**Author:** ![nirav.makwana](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/nirav.makwana/32/8032_2.png) [@nirav.makwana](https://forum.katalon.com/u/nirav.makwana)\
**Post date:** [September 18, 2019, 7:50am UTC](https://forum.katalon.com/t/how-to-define-a-global-variable-on-the-fly-and-access-tit-through-different-suites-in-the-collection/32575/3 "2019-09-18T07:50:26Z")

</div>

Thanks for the reply.  
I did the same work around as per you suggestion.  
Thanks for the help.
