Xavi Duro
Xavi Duro

Reputation: 26

Pentaho/Kettle Javascript: Calling a field dynamically

I'm trying to retrieve the value of a field but I need the name of the field to be built dynamically.

Imagine I have the people born by day in the last year in columns named as the day number. So you have columns like DAY_1, DAY_2, DAY_3 and so on. And the values are 5, 12, 33... I want to do a loop over all those values and for that I built the field name dinamycally:

var column_name = "DAY_"+i

So later on I can do my loop increasing i one by one.

Problem is that javascript does not recognize DAY_i as a valid input field so I cannot retrieve the value of the field, Pentaho handles it as a string.

Upvotes: 0

Views: 2470

Answers (2)

Carlos Santana
Carlos Santana

Reputation: 1

For get input field value dynamically you can used this code:

var column_index = getInputRowMeta().indexOfValue(column_name);
var valor = row[column_index];
var valorString = null;

if (valor != null) {
  var valueMeta = getInputRowMeta().getValueMeta(column_index);
  valorString = valueMeta.getString(valor);
}

The variable "valorString" will contain the desired value.

Upvotes: 0

Andrei Luksha
Andrei Luksha

Reputation: 1042

In javascript step there is an array variable row. You can lookup for an index of your column using getInputRowMeta().indexOfValue("DAY_" + i) and then use that index to get value of the field in current row:

var column_name = "DAY_" + i;
var column_index = getInputRowMeta().indexOfValue(column_name);
var column_value = row[column_index];

Please note, that column_value will reference to an Object of some Java type (e.g. java.lang.String), so javascript String methods will not work on this value. You will need to convert the value to a javascript type or use java methods.

Upvotes: 3

Related Questions