Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Toronto Sept 9th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Changing the value of a variable set in tGlobalVar

Hi guys,
If I have a variable set using tGlobalVar example name and it's value is an empty string. How can I modify it using tJava?
and if I ever declare a variable in tJava is it possible that can I call that variable in other components? If yes how?

Thanks,
drixs
Labels (3)
13 Replies
Anonymous
Not applicable
Author

Hi
In tJava, you can set a value to the global variable which you have defined on tGlobalVar, for example:
tSetGlobalVar
|
onsuojobok
|
tJava
on tSetGlobalVar, add a new variable let's call it "myKey" and set its value as "oldValue"
on tJava, set a new value to global variable 'myKey" like this:
globalMap.put("mykey", "newValue")
You can use this variable in other component later as long as you declare a global variable before using it, call the variables in other component later like:
(String)globalMap.get("myKey") //the data type of value is String
(Integer)globalMap.get("myKey") //the data type of value is Integer/int
Best regards
Shong
Anonymous
Not applicable
Author

Hi Shong,
Just want to have a clarification.
For example I've declared a variable in tJava:
int record = 0;
can i call this variable in other component?
Anonymous
Not applicable
Author

Hi
No, only a global variable can be accessed on other component. As you done, define a global variable using tSetglobalVar component or use this expression:
globalMap.put("key","value")
Best regards
Shong
Anonymous
Not applicable
Author

Hi shong,
I have some problem here.
I declare a variable using a tSetGlobalVar : Key: PrevComb | Value:"novalue"
and here is my job outline
tSetGlobalVar
|
(Main)
|
tFlowToIterate
|
(Iterate)
|
tJava
In my tJava , I have a condition ((String)globalMap.get("CurrentComb")) != ((String)globalMap.get("PrevComb")) and if it's true it will be performing this:
globalMap.put("PrevComb" , ((String)globalMap.get("CurrentComb")))
this means the CurrentComb value will be stored in PrevComb, am i right?
with this try to simulate in the next run it will be false because they have the same value, right?
But in my case on the next the result is true, it seems that PrevComb value is still "novalue".
Help me out thanks!
Anonymous
Not applicable
Author

Just done a test and there's no value change in the variable
Anonymous
Not applicable
Author

Hi
In my tJava , I have a condition ((String)globalMap.get("CurrentComb")) != ((String)globalMap.get("PrevComb"))

To determine whether one string is equals to another one, you should the use the method String.equals("anotherString"), so the expression should be:
((String)globalMap.get("CurrentComb")).equals(((String)globalMap.get("PrevComb")))
BTW, for the numeric type, such as Integer/int, you can use ==.
Best regards
Shong
Anonymous
Not applicable
Author

Oh I see , Noted
How about this issue:
I declare a variable using a tSetGlobalVar : Key: PrevComb | Value:"novalue"
and here is my job outline
tSetGlobalVar
|
(Main)
|
tFlowToIterate
|
(Iterate)
|
tJava

globalMap.put("PrevComb" , ((String)globalMap.get("CurrentComb")))
this means the CurrentComb value will be stored in PrevComb, am i right?
with this try to simulate in the next run it will be false because they have the same value, right?
But in my case on the next the result is true, it seems that PrevComb value is still "novalue".
Anonymous
Not applicable
Author

Oh I see , Noted
How about this issue:

;(, before you post this message, I think you shall to change the method equlas() as I said and test the job again.
Best regards
Shong
Anonymous
Not applicable
Author

;(, before you post this message, I think you shall to change the method equlas() as I said and test the job again.

Omg! Sorry shong!! Wasnt able to reply it clearly because it's almost end of office hours.
Thanks for the .equals() shong!
one more question:
in this situation:
tGlobalSetVar:
key | value
count | 0

tGlobalSetVar
|
Main
|
tFlowToIterate
|
iterate
|
tJava

in tJava
globalMap.put("count" , ((Integer))globalMap.get("count") + 1 );

This is similar with count++;
right?