Hitachi Vantara Pentaho Community Wiki
Child pages
  • Modified Java Script Value

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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];
}

...

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, ...).

...

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.

...

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;
    }
}

...

Make sure to use the following construct:

Code Block

string.equals(otherString)

...

You can also use the following method if you want to ignore case differences:

Code Block

string.equalsIgnoreCase(otherString)

...

Whatever may be, in case you are having trouble using == or switch/case on values that you know are integer in nature, use the following constructs:

Code Block

parseInt(num)==parseInt(num2)

or

Code Block

switch(parseInt(valuename))
{
case 1:
case 2:
case 3:
 strvalueswitch = "one, two, three";
 break;
case 4:
 strvalueswitch = "four";
 break;
default:
 strvalueswitch = "five";
}

...

If you want to filter rows, i.e. remove rows from the output, you can set the trans_Status variable:

Code Block

trans_Status = CONTINUE_TRANSFORMATION
if (/* add your condition here */) trans_Status = SKIP_TRANSFORMATION

All rows matching the condition are removed from the output.

Blog "Squeezing the most out of the JavaScript Step in Pentaho Kettle":
http://type-exit.org/adventures-with-open-source-bi/2010/06/squeezing-the-most-out-of-the-javascript-step-in-pentaho-kettle/