Search code examples
javascriptpentahokettle

Print For-Loop results in a table


I have 10 rows of data on my input step, i transform them in a for-loop and i should get more than 10 rows, but in this case i get the last transform of each iteration that the loop have for each data

I tried to use appendToFile() but the result data is not useful and pentaho read it as a unique header On my alert() method i can see that the for loop transform the data.

var PERIODO = 2
var i
var fecha_final
//var ruta_acess ="D:\TEST.accdb"
//var contenido
////var contenido2
//var arreglo_completo
for (i=0; i<=PERIODO; i++){

fecha_final = dateAdd(FECHA_INICIO,"d",i)
Alert(i)
}

As I show in the below photo i get only 10 records and in the other photo appears the result that i want that are the results data of each iteration of the for-loop

Modified JavaScript value photo:

Modified JavaScript value photo

Expected result:

Expected result

Obtained result:

Obtained result


Solution

  • For loops are not really a thing in PDI. Transformations work on sets of rows that flow through the steps, so it's best for performance and stability to use that mindset.

    In your scenario each incoming row should end up as three copies, but with different calculated values based on a single new field (with values 0,1,2).

    The way to do this in PDI is with a Join rows (cartesian product) step. It takes two sets of input rows and outputs a row for every combination of input rows, possibly filtered by defining a key field that has to match. So if you have 10 rows in the main input and 3 rows in the second, it will output 30 rows.

    You will first need to create a data grid as the second input. Define a single integer field, name it something clear and on the second tab fill three rows with 0, 1 and 2 respectively.

    Connect both inputs to the Join rows step. You don't need to configure any matching key. The output of the Join step will be three rows for each input row, one with each of the values 0, 1, 2. Connect that output to a Calculator step and use the calculation Date A + B days to replace the logic from your javascript step.

    enter image description here