Search code examples
javascriptpentahopentaho-spoonpentaho-data-integration

Read data from previous step in JavaScript step in Pentaho


I have a column in csv file named Account Rating and I am using Modified Java Script Value step to create a new column using the rating column. I want following new column:

Account Rating   New_Rating
---------------------------
Cold             ABC
Warm             DEF
Hot              XYZ

I tried following script in Java Script step:

var rating = getInputRowMeta().getString("Account Rating");
var new_rating = "Not assigned";

if (rating === 'Cold')
    new_rating = 'ABC';
else if (rating === 'Warm')
    new_rating = 'DEF';
else if (rating === 'Hot')
    new_rating = 'XYZ';

When I execute it, I am getting following error:

Can't find method org.pentaho.di.core.row.RowMeta.getString(string)

Can anyone help me with my script?


Solution

  • If you want to keep the Field Name which contains SPACE and can't be used as a JavaScript variable name, then you can modify your original code to first retrieve the index of the field from the RowMetaInterface object, and then get the corresponding value from that index:

    var mapping = {
        'Cold' : 'ABC'
    ,   'Warm' : 'DEF'
    ,   'Hot' : 'XYZ' 
    };
    
    var idx = getInputRowMeta().indexOfValue("Accounting Rating");
    var rating = row[idx];
    var new_rating = mapping[rating] || 'Not assigned';
    

    More info: https://help.pentaho.com/Documentation/6.0/0R0/0V0/010/000/020/010