Hitachi Vantara Pentaho Community Wiki
Child pages
  • JavaScript Step FAQ

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

What does the compatibility switch do?

There are two version of the javascript engine: the 2.5 version and the 3 version. If "compatibility mode" is checked (and by default it is), javascript works like it did in version 2.5. Obviously the new version should be used if possible so uncheck "compatibility mode" if you can.

The big difference between the two versions is that in 2.5, value objects are directly modifiable and their type can be changed (a date variable can be converted into a string). This can cause subtle bugs (see migration 2.5->3.0 doc for more details). Because this is no longer possible in the 3.0 version, the javascript should also be faster.

For more details, read the migration 2.5->3.0 doc.

How to check for the existence of fields in rows (with compatibility on)?

The following snippet (using compatibility switched on) will let you check this. But keep in mind that you can not mix rows in PDI, all rows flowing over a single hop have to have the same number of fields, which have to be of the same name and type.

The snippet:

Code Block
var idx = row.searchValueIndex("lookup");
if ( idx < 0 )
{
   var lookupValue = 0;
}
else
{
   var lookupValue = row.getValue(idx);
}

The same snippet without compatibility switched on:

Code Block
var idx = getInputRowMeta().indexOfValue("lookup");
if ( idx < 0 )
{
   var lookupValue = 0;
}
else
{
   var lookupValue = row[idx];
} 

How to add a new field in a row

Note up front that the order in which fields are added to a row is important. Always add fields in the same order to keep the structure of the row coherent.

Now to add a field:

  • Define it as "var" in the source and add it as a field in the fields table in the lower half of the JavaScript dialog.

 

How to modify values (with compatibility off)

In 3.0+, the preferred way to change an input value (and potentially its type) is to create a new variable and output it using the "fields" list underneath the main javascript textarea. Then in a separate 'Select values' step, replace the old variable with the new one. While this is slightly less elegant than in 2.5, the code should be faster and safer.

How to modify values (with compatibility on)

When compatibility is switched on, use setValue on the input field as follows (assuming field1 is a field in the input row):

Code Block
field1.setValue(100);

setValue() takes all possible types that can be used in PDI (also String, Dates, ...).

How to use something like NVL in JavaScript?

You can use the following construction (to get something like nvl(a, '0')) with the compatibility switch on:

Code Block
var a;
if ( fieldname.isNull() )
{
    a = '0';
}
else
{
    a = fieldName.getString();
}

and you can also use:

Code Block
fieldName.nvl('1'); 

which would replace the value of fieldName with the value of '1' if fieldName is null.

Example of how to split fields

In a field I have merchant code containing numbers and characters, ex. "12345McDonalds". I want to split it but the field doesn't have a consistent layout and I want to split the first part which is numeric from the second part.

Use following piece of JavaScript, Merchant_Code is the name of the input field

Code Block
java;

var str = Merchant_Code.getString();

var code = "";
var name = "";

for (i = 0; i < str.length(); i++ )
{
    c = str.charAt(i);
    if ( ! java.lang.Character.isDigit(c) )
    {
        code = str.substring(0, i);
        name = str.substring(i);
        Alert("code="+code+", name="+name);
        break;
    }
} 

The Alert() is just to show the fields of course. After the outer for loop you could add code and name in new separate fields e.g.