GlobalVariable value update

Hi, I am trying to update GlobalVariable value using list but its not working can anyone let me know the solution for this

int TotalDestinoDB = 3
ArrayList DestinoAccDB = new ArrayList(Arrays.asList(“4562457893”, “451475746032562”, “45582045”, “NA”, “NA”, “NA”))
ArrayList GlobalVariableBeforeDB = new ArrayList(Arrays.asList(GlobalVariable.destino_before_db0, GlobalVariable.destino_before_db1, GlobalVariable.destino_before_db2, GlobalVariable.destino_before_db3, GlobalVariable.destino_before_db4, GlobalVariable.destino_before_db5))

for(int count = 0; count < TotalDestinoDB; count++) {
if(DestinoAccDB[count].length() > 10 && DestinoAccDB[count] != “NA”) {
// Tarjeta DB Query
KeywordUtil.logInfo(“Tarjeta”)
//GlobalVariableBeforeDB[count] = GlobalVariable.destino_before_db
} else if(DestinoAccDB[count] != “NA”) {
//CustomKeywords.‘helper.functionsupdate.ConsultaBalance_Destino’(DestinoAccDB[count])
GlobalVariable.destino_before_db = “Joool”
GlobalVariableBeforeDB[count] = GlobalVariable.destino_before_db
} else {
KeywordUtil.logInfo(“NO ACCOUNT”)
}
}

KeywordUtil.logInfo(GlobalVariable.destino_before_db)
KeywordUtil.logInfo(GlobalVariable.destino_before_db0)
KeywordUtil.logInfo(GlobalVariable.destino_before_db1)
KeywordUtil.logInfo(GlobalVariable.destino_before_db2)
KeywordUtil.logInfo(GlobalVariable.destino_before_db3)
KeywordUtil.logInfo(GlobalVariable.destino_before_db4)
KeywordUtil.logInfo(GlobalVariable.destino_before_db5)

Here in GlobalVariable.destino_before_db0 the value should be “Joool” but I am getting no value
@kazurayam

Maybe you have a spelling mistake if you expect destino_before_db0 to have any value. You assign the variable to the ArrayList, but then you do not have any initialization of it. Then you display it with the logInfo in the last part.

Maybe you want:
GlobalVariable.destino_before_db0 = "Joool"

2 Likes

@vaibhavp

Please use “Code Formatting” syntax for better readability.

@vaibhavp

KeywordUtil.logInfo(GlobalVariable.destino_before_db)
KeywordUtil.logInfo(GlobalVariable.destino_before_db0)
KeywordUtil.logInfo(GlobalVariable.destino_before_db1)
KeywordUtil.logInfo(GlobalVariable.destino_before_db2)
KeywordUtil.logInfo(GlobalVariable.destino_before_db3)
KeywordUtil.logInfo(GlobalVariable.destino_before_db4)
KeywordUtil.logInfo(GlobalVariable.destino_before_db5)

This portion suggests that you should have 7 GlobalVariables defined. How did you defined the GlobalVaraible.destino_before_db and the GlobalVariable.destino_before_db0 in the Execution Profile default? Do you have the default Execution Profile something like this? :

Please show the screenshot of your Exectuion Profile.


I have a doubt; you haven’t defined them in any of Execution Profile. Therefore there are no GlobalVariables defined in fact.

wrong:

ArrayList DestinoAccDB = new ArrayList(Arrays.asList("4562457893", "451475746032562", "45582045", "NA", "NA", "NA"))
...
for(int count = 0; count < TotalDestinoDB; count++) {
	if(DestinoAccDB[count].length() > 10 && 

right:

ArrayList<String> DestinoAccDB = ["4562457893", "451475746032562", "45582045", "NA", "NA", "NA"]
...
for(int count = 0; count < TotalDestinoDB; count++) {
	if(DestinoAccDB[count].length() > 10 && 

Your code looks too messy to me. Especially, variables are named inappropriately. Inappropriate variable names confuse me.

Also you seem do not understand the syntax of Groovy. I tried to rewrite you code more “Groovy”.

import com.kms.katalon.core.util.KeywordUtil
import internal.GlobalVariable

ArrayList<String> beforeDB =
	Arrays.asList(
		GlobalVariable.destino_before_db0,
		GlobalVariable.destino_before_db1,
		GlobalVariable.destino_before_db2,
		GlobalVariable.destino_before_db3,
		GlobalVariable.destino_before_db4,
		GlobalVariable.destino_before_db5)

ArrayList<String> destinoAccDB = ["4562457893", "451475746032562", "45582045"]
	
destinoAccDB.eachWithIndex { account, index ->
	if (account.length() > 10 && account != "NA") {
		// Tarjeta DB Query
		KeywordUtil.logInfo("Tarjeta")
	} else if (account != "NA") {
		// GlobalVariable.destino_before_db must be defined to have the value "Joool"
		beforeDB[index] = GlobalVariable.destino_before_db
	} else {
		KeywordUtil.logInfo("NO ACCOUNT")
	}
}

KeywordUtil.logInfo(GlobalVariable.destino_before_db)
KeywordUtil.logInfo(GlobalVariable.destino_before_db0)
KeywordUtil.logInfo(GlobalVariable.destino_before_db1)
KeywordUtil.logInfo(GlobalVariable.destino_before_db2)
KeywordUtil.logInfo(GlobalVariable.destino_before_db3)
KeywordUtil.logInfo(GlobalVariable.destino_before_db4)
KeywordUtil.logInfo(GlobalVariable.destino_before_db5)

This printed this:

In the end, I do not understand your program.

I think that your code is not doing this.

I do not see your intention (what you want to do) out of the code you shared.

1 Like

Hi @kazurayam I defined each global variable in default profile my doubt is like
If I store global variable in list

ArrayList GlobalVariableBeforeDB = new ArrayList(Arrays.asList(GlobalVariable.destino_before_db0, GlobalVariable.destino_before_db1, GlobalVariable.destino_before_db2, GlobalVariable.destino_before_db3, GlobalVariable.destino_before_db4, GlobalVariable.destino_before_db5))

then If some condition matches then I take value from GlobalVariable.destino_before_db and then I want to store into any one of the global variable which is stored in List like
GlobalVariableBeforeDB[count] = GlobalVariable.destino_before_db. But when I do that and try to print I couldn’t get the value there

sorry, I can’t understand it.
I would quit.

Here if it goes in that else if block then expected result I wanted is the value which is in “GlobalVariable.destino_before_db” should get stored in “GlobalVariable.destino_before_db0”

GlobalVariableBeforeDB[count] = GlobalVariable.destino_before_db where {count = 0} then
i.e,
GlobalVariable.destino_before_db0 = GlobalVariable.destino_before_db

but when I print the GlobalVariable.destino_before_db0 value is blank
@kazurayam

You add value to the list called GlobalVariableBeforeDB to position 0 and not to the original GlobalVariable.destino_before_db0. (the latter is only the default value in the list at pos 0 that you intend to modify later)

You should find GlobalVariable.destino_before_db (“ABCD”) in GlobalVariableBeforeDB[0]

Yes in GlobalVariableBeforeDB at position 0 I kept GlobalVariable.destino_before_db0 so after that if I do like
GlobalVariableBeforeDB[0] = GlobalVariable.destino_before_db
which is equivalent to
GlobalVariable.destino_before_db0 = GlobalVariable.destino_before_db

So after that the value which is stored in GlobalVariable.destino_before_db should get it GlobalVariable.destino_before_db0 but the result I am getting is blank

It is not equivalent to each other.
You do not modify the value of GlobalVariable.destino_before_db0.

ok it there any way I can achieve like this like storing global variable into list and assigning some value to it in runtime so I can access that from list or can we iterate through global variable if I have 4 global variable named
GlobalVariable.Value1, GlobalVariable.Value2, GlobalVariable.Value3, GlobalVariable.Value4 so is there anyway I can iterate through loop like GlobalVariable.Value[i]

You can define a GlobalVariable as a List in your Profile and use it “assigning some value to it in runtime so [you] can access that from list”.

I don’t suggest going with the 4 GlobalVariables because you would need a Case block (switch case) to use them as you iterate through any loop. You can’t concatenate a number onto the end of a variable on the left side in your equate statement.

e.g. sample Case block:

	switch(irow) {
	   case 0:
		   GlobalVariable.Value0 = ...
		   break;
	   case 1:
		   GlobalVariable.Value1 = ...
		   break;
	   case 2:
		   GlobalVariable.Value2 = ...
		   break;
	   case 3:
		   GlobalVariable.Value3 = ...
		   break;
	   case 4:
		   GlobalVariable.Value4 = ...
		   break;
	   default:
		   WebUI.comment('oops')
		   break;
	}

Again, go with the List.

Ok thanks for way around