"Selected row has index -1" means the table has no selected record at the moment you ask. Two reasons are common: reading in the wrong event, or reading through the wrong API. The UI Toolkit table gives you the record directly:
// Table control (UI Toolkit), single select
// 1. In the table's "On row selected" event (fires with the selected record):
// event signature: (row, record, index) -> record is the bound list element
${Details}.setData(record); // e.g. show it in a Data / form section
console.log("selected index", index, "id", record.id);
// 2. From a button handler afterwards:
var t = ${Orders}; // control id of the table
var rec = t.getSelectedRecord ? t.getSelectedRecord() : t.getSelectedRecords()[0];
var idx = t.getSelectedIndex ? t.getSelectedIndex() : t.getSelectedIndices()[0];
if (idx < 0) { alert("Select a row first"); return; }
var byIndex = t.getRecord(idx); // the record for any index
// the bound list itself: ${Orders}.getData() or the coach variable
var list = ${Orders}.getData(); var same = list[idx];Why -1 happens: the table is bound to a list that is re-set (for example a service result assigns a new list) - every new list clears the selection; a click inside a cell control (button, checkbox) does not select the row on older toolkits unless Select on click is on; on the Multi selection mode there is no single index, use getSelectedIndices().
Also note the table shows the filtered / sorted order: getSelectedIndex() is the index in the displayed table, getSelectedRecord() is always the real record - prefer the record over the index and look it up by an id field in the bound list.
References