How inster new rows in table using variable array type?

Hello, I have two tables, the first table contains a list of product to user can add to second table but this action add not function correctly.

I create a variable array type and load new rows bu I don´t now how pass this variable to second table and how validate if this variable is undefined or empty o is different of empty and undefined

my code is

function addRowToTable() {
const clickedRow = tblitems2.selectedSourceRow;
console.log(rowsCotiza.length);
if(rowsCotiza.id == undefined){
console.log("Es indefinido");
}
rowsCotiza=[];

const select ={
ITEM: clickedRow.ITEM,
Desc: clickedRow.LOCAL_SHORT_DESC
};

rowsCotiza.push(select);
console.log(rowsCotiza);

rowsCotiza = [...tblCotizacion.data, rowsCotiza];

tblCotizacion.data = rowsCotiza;

// rows.push(select);
tblCotizacion.data = rowsCotiza;

return rowsCotiza;

}

return addRowToTable();

Hi @Winston_Altamirano - Welcome to the forums!

If I'm understanding correctly, you're trying to have items from the first table populate the second table upon button click. You're on the right track with using a variable that's set to an array for initial value.
In order to set the variable you have to use the methods provided within Retool (instead of 'push' like in your code example) - variable.setValue() or variable.setIn(). The setVariable() method will overwrite the existing value of the variable, and the setIn() method will set the value within the variable at a given index.
Example: Instead of rowsCotiza=[], you can use rowsCotiza.setValue([]) to reset the variable.

I'm not trying to discourage you from using your script, but here's an example script that will add the selected item to the end of the variable array (table2 is my table containing all the rows, which has an ID field and a Name field

//Get selected row
const clickedRow = table2.selectedSourceRow;
//Add ID and Name of clicked item to the end of the array
rowsCotiza.setIn(
  [rowsCotiza.value.length], 
  {
     id: clickedRow.id,
     name: clickedRow.customer_name
  }
)

Then set the Data Source for your second table to rowsCotiza, and it should populate.
retool_tblselect