Can I use dynamic variable names into Groovy scripts

Hello Everybody,

I use Bonita 6.4,

I would like to know if it’s possible to use dynamic variable names into a groovy script?
I would like to use it, for exemple into a page validation groovy script, to validate into a loop multiple fields at the same time.

For exemple, I have 3 fields named field_validation1, field_validation2 and field_validation3
Is it a way to do something like that ? : (the syntax is wrong, it’s just an exemple)
var total_number_fields = 3;
for(var i=1;i<=total_number_fields;i++){
var my_value = ${field_validation}+i; // get dynamically the value and store it into an array
}

I want to do this because the number of fields can change, and I don’t want to duplicate a lot of validation code when the number of fields will change, I just want to update the variable total_number_fields.

Thanks a lot!

Nope, I’ve tried it…

Just use a Java List. It can be 0 to x long meeting your requirement for different sizes of list.

To get the size of a list just do no need for a loop:

var sizeOfList = myList.size();

Just reading again…how do you get a variable number of variables? That’s not really possible is it?

regards
Seán

Hi Sean McP,

Thanks for your response.
My question is mainly on the usage of dynamic variable name.
The loop is just an exemple. For exemple if it’s possible to use dynamic variable name, we can do something like that to loop all fields with the same name patern (the syntax is wrong):
def i = 1;
while(isset(${field_validation}+i)){
//do something
i++
}

But if you said it’s not possible to use dynamic variable name… I must duplicate some code… :frowning:

Believe me I would love to be able to do this…I used to in a different language and it was great!

But alas no more, modern languages do not allow this type of thing.

Then again you could use a BDO which keeps the same name pattern and can be referenced by id…

regards
Seán